How to while loop a list (reading each line untill the end) without saving the list as a file?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
The command below will give me a list of users not with a 1 day min password life. This works correctly.
awk -F: '$4 < 1 print $1' /etc/shadow
I want to take this list, and run...
chage -m 1 $p
where $p is the username on each line.
Normally, i would save this list as a file, and then run a while loop against it. See below for my full command to change all users to 1 day min password.
mkdir ./tmp
awk -F: '$4 < 1 print $1' /etc/shadow > ./tmp/usernamelist
while read p; do
chage -m 1 $p
done <./tmp/usernamelist
rm -rf ./tmp/usernamelist
Is there a way to do the same thing logically, but without having to save a file? Is this best practice?
I would think there would be a way to do this with a pipe command, but i can't find a proper way to structure it.
bash shell-script
add a comment |Â
up vote
0
down vote
favorite
The command below will give me a list of users not with a 1 day min password life. This works correctly.
awk -F: '$4 < 1 print $1' /etc/shadow
I want to take this list, and run...
chage -m 1 $p
where $p is the username on each line.
Normally, i would save this list as a file, and then run a while loop against it. See below for my full command to change all users to 1 day min password.
mkdir ./tmp
awk -F: '$4 < 1 print $1' /etc/shadow > ./tmp/usernamelist
while read p; do
chage -m 1 $p
done <./tmp/usernamelist
rm -rf ./tmp/usernamelist
Is there a way to do the same thing logically, but without having to save a file? Is this best practice?
I would think there would be a way to do this with a pipe command, but i can't find a proper way to structure it.
bash shell-script
Or just callsystem
from within awk, since reading shadow requires root anyway.
â Jeff Schaller
Jul 20 at 2:32
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The command below will give me a list of users not with a 1 day min password life. This works correctly.
awk -F: '$4 < 1 print $1' /etc/shadow
I want to take this list, and run...
chage -m 1 $p
where $p is the username on each line.
Normally, i would save this list as a file, and then run a while loop against it. See below for my full command to change all users to 1 day min password.
mkdir ./tmp
awk -F: '$4 < 1 print $1' /etc/shadow > ./tmp/usernamelist
while read p; do
chage -m 1 $p
done <./tmp/usernamelist
rm -rf ./tmp/usernamelist
Is there a way to do the same thing logically, but without having to save a file? Is this best practice?
I would think there would be a way to do this with a pipe command, but i can't find a proper way to structure it.
bash shell-script
The command below will give me a list of users not with a 1 day min password life. This works correctly.
awk -F: '$4 < 1 print $1' /etc/shadow
I want to take this list, and run...
chage -m 1 $p
where $p is the username on each line.
Normally, i would save this list as a file, and then run a while loop against it. See below for my full command to change all users to 1 day min password.
mkdir ./tmp
awk -F: '$4 < 1 print $1' /etc/shadow > ./tmp/usernamelist
while read p; do
chage -m 1 $p
done <./tmp/usernamelist
rm -rf ./tmp/usernamelist
Is there a way to do the same thing logically, but without having to save a file? Is this best practice?
I would think there would be a way to do this with a pipe command, but i can't find a proper way to structure it.
bash shell-script
asked Jul 20 at 1:52
TrevorKS
1068
1068
Or just callsystem
from within awk, since reading shadow requires root anyway.
â Jeff Schaller
Jul 20 at 2:32
add a comment |Â
Or just callsystem
from within awk, since reading shadow requires root anyway.
â Jeff Schaller
Jul 20 at 2:32
Or just call
system
from within awk, since reading shadow requires root anyway.â Jeff Schaller
Jul 20 at 2:32
Or just call
system
from within awk, since reading shadow requires root anyway.â Jeff Schaller
Jul 20 at 2:32
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Either with a pipe
awk -F: '$4 < 1 print $1' /etc/shadow |
while read p; do
chage -m 1 "$p"
done
or with process substitution
while read p; do
chage -m 1 "$p"
done < <(awk -F: '$4 < 1 print $1' /etc/shadow)
So you can just pipe data into the while loop like that and it'll read it as if it is reading a file? I confirm this works.
â TrevorKS
Jul 20 at 2:02
@TrevorKS yesread
reads the standard input stream, whether that comes by input redirection from a file (or process) or via a pipe
â steeldriver
Jul 20 at 2:15
add a comment |Â
up vote
0
down vote
awk -F: '$4 < 1 print $1' /etc/shadow | xargs -r -L 1 chage -m 1
The -L 1
ensures that each line of the awk output is passed to a separated chage invocation, and the -r
ensures that chage is not run executed if awk doesn't output anything (which, admittedly, is quite unlikely in this case).
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Either with a pipe
awk -F: '$4 < 1 print $1' /etc/shadow |
while read p; do
chage -m 1 "$p"
done
or with process substitution
while read p; do
chage -m 1 "$p"
done < <(awk -F: '$4 < 1 print $1' /etc/shadow)
So you can just pipe data into the while loop like that and it'll read it as if it is reading a file? I confirm this works.
â TrevorKS
Jul 20 at 2:02
@TrevorKS yesread
reads the standard input stream, whether that comes by input redirection from a file (or process) or via a pipe
â steeldriver
Jul 20 at 2:15
add a comment |Â
up vote
2
down vote
accepted
Either with a pipe
awk -F: '$4 < 1 print $1' /etc/shadow |
while read p; do
chage -m 1 "$p"
done
or with process substitution
while read p; do
chage -m 1 "$p"
done < <(awk -F: '$4 < 1 print $1' /etc/shadow)
So you can just pipe data into the while loop like that and it'll read it as if it is reading a file? I confirm this works.
â TrevorKS
Jul 20 at 2:02
@TrevorKS yesread
reads the standard input stream, whether that comes by input redirection from a file (or process) or via a pipe
â steeldriver
Jul 20 at 2:15
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Either with a pipe
awk -F: '$4 < 1 print $1' /etc/shadow |
while read p; do
chage -m 1 "$p"
done
or with process substitution
while read p; do
chage -m 1 "$p"
done < <(awk -F: '$4 < 1 print $1' /etc/shadow)
Either with a pipe
awk -F: '$4 < 1 print $1' /etc/shadow |
while read p; do
chage -m 1 "$p"
done
or with process substitution
while read p; do
chage -m 1 "$p"
done < <(awk -F: '$4 < 1 print $1' /etc/shadow)
answered Jul 20 at 1:56
steeldriver
30.8k34877
30.8k34877
So you can just pipe data into the while loop like that and it'll read it as if it is reading a file? I confirm this works.
â TrevorKS
Jul 20 at 2:02
@TrevorKS yesread
reads the standard input stream, whether that comes by input redirection from a file (or process) or via a pipe
â steeldriver
Jul 20 at 2:15
add a comment |Â
So you can just pipe data into the while loop like that and it'll read it as if it is reading a file? I confirm this works.
â TrevorKS
Jul 20 at 2:02
@TrevorKS yesread
reads the standard input stream, whether that comes by input redirection from a file (or process) or via a pipe
â steeldriver
Jul 20 at 2:15
So you can just pipe data into the while loop like that and it'll read it as if it is reading a file? I confirm this works.
â TrevorKS
Jul 20 at 2:02
So you can just pipe data into the while loop like that and it'll read it as if it is reading a file? I confirm this works.
â TrevorKS
Jul 20 at 2:02
@TrevorKS yes
read
reads the standard input stream, whether that comes by input redirection from a file (or process) or via a pipeâ steeldriver
Jul 20 at 2:15
@TrevorKS yes
read
reads the standard input stream, whether that comes by input redirection from a file (or process) or via a pipeâ steeldriver
Jul 20 at 2:15
add a comment |Â
up vote
0
down vote
awk -F: '$4 < 1 print $1' /etc/shadow | xargs -r -L 1 chage -m 1
The -L 1
ensures that each line of the awk output is passed to a separated chage invocation, and the -r
ensures that chage is not run executed if awk doesn't output anything (which, admittedly, is quite unlikely in this case).
add a comment |Â
up vote
0
down vote
awk -F: '$4 < 1 print $1' /etc/shadow | xargs -r -L 1 chage -m 1
The -L 1
ensures that each line of the awk output is passed to a separated chage invocation, and the -r
ensures that chage is not run executed if awk doesn't output anything (which, admittedly, is quite unlikely in this case).
add a comment |Â
up vote
0
down vote
up vote
0
down vote
awk -F: '$4 < 1 print $1' /etc/shadow | xargs -r -L 1 chage -m 1
The -L 1
ensures that each line of the awk output is passed to a separated chage invocation, and the -r
ensures that chage is not run executed if awk doesn't output anything (which, admittedly, is quite unlikely in this case).
awk -F: '$4 < 1 print $1' /etc/shadow | xargs -r -L 1 chage -m 1
The -L 1
ensures that each line of the awk output is passed to a separated chage invocation, and the -r
ensures that chage is not run executed if awk doesn't output anything (which, admittedly, is quite unlikely in this case).
answered Jul 20 at 4:57
user1934428
34118
34118
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f457325%2fhow-to-while-loop-a-list-reading-each-line-untill-the-end-without-saving-the-l%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Or just call
system
from within awk, since reading shadow requires root anyway.â Jeff Schaller
Jul 20 at 2:32