How to while loop a list (reading each line untill the end) without saving the list as a file?

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question



















  • Or just call system from within awk, since reading shadow requires root anyway.
    – Jeff Schaller
    Jul 20 at 2:32
















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.







share|improve this question



















  • Or just call system from within awk, since reading shadow requires root anyway.
    – Jeff Schaller
    Jul 20 at 2:32












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.







share|improve this question











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.









share|improve this question










share|improve this question




share|improve this question









asked Jul 20 at 1:52









TrevorKS

1068




1068











  • 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















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










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)





share|improve this answer





















  • 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

















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).






share|improve this answer





















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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)





    share|improve this answer





















    • 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














    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)





    share|improve this answer





















    • 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












    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)





    share|improve this answer













    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)






    share|improve this answer













    share|improve this answer



    share|improve this answer











    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 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
















    • 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















    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












    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).






    share|improve this answer

























      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).






      share|improve this answer























        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).






        share|improve this answer













        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).







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 20 at 4:57









        user1934428

        34118




        34118






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay