Bash script copy file to user's (wildcard) home dir

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I need to copy /root/nbu/file1.sh to every user's home directory if user's id is even number.



Trying to execute following script:



#!/bin/bash
cat /etc/passwd | while read LINE
do
username=$(awk -v var="$LINE" -F: '
if ($3 % 2 == 0)
print $1
')
cp /root/nbu/file1.sh ~"$username"
done


And it does nothing. If I echo cp /root/nbu/file1.sh ~"$username" command and execute it directly in the script, it works.



I guess the problem is that ~ gets expanded in the bash script, but can't figure out how to solve this problem.



Thank you in advance.







share|improve this question


















  • 1




    Just a note, ~ is tilde, and $username is a variable; there are no "wildcards" present here. ... Unless one shows up to answer :)
    – Jeff Schaller
    Mar 7 at 17:52










  • Relating only: unix.stackexchange.com/questions/270274/…
    – Jeff Schaller
    Mar 7 at 17:56














up vote
2
down vote

favorite












I need to copy /root/nbu/file1.sh to every user's home directory if user's id is even number.



Trying to execute following script:



#!/bin/bash
cat /etc/passwd | while read LINE
do
username=$(awk -v var="$LINE" -F: '
if ($3 % 2 == 0)
print $1
')
cp /root/nbu/file1.sh ~"$username"
done


And it does nothing. If I echo cp /root/nbu/file1.sh ~"$username" command and execute it directly in the script, it works.



I guess the problem is that ~ gets expanded in the bash script, but can't figure out how to solve this problem.



Thank you in advance.







share|improve this question


















  • 1




    Just a note, ~ is tilde, and $username is a variable; there are no "wildcards" present here. ... Unless one shows up to answer :)
    – Jeff Schaller
    Mar 7 at 17:52










  • Relating only: unix.stackexchange.com/questions/270274/…
    – Jeff Schaller
    Mar 7 at 17:56












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I need to copy /root/nbu/file1.sh to every user's home directory if user's id is even number.



Trying to execute following script:



#!/bin/bash
cat /etc/passwd | while read LINE
do
username=$(awk -v var="$LINE" -F: '
if ($3 % 2 == 0)
print $1
')
cp /root/nbu/file1.sh ~"$username"
done


And it does nothing. If I echo cp /root/nbu/file1.sh ~"$username" command and execute it directly in the script, it works.



I guess the problem is that ~ gets expanded in the bash script, but can't figure out how to solve this problem.



Thank you in advance.







share|improve this question














I need to copy /root/nbu/file1.sh to every user's home directory if user's id is even number.



Trying to execute following script:



#!/bin/bash
cat /etc/passwd | while read LINE
do
username=$(awk -v var="$LINE" -F: '
if ($3 % 2 == 0)
print $1
')
cp /root/nbu/file1.sh ~"$username"
done


And it does nothing. If I echo cp /root/nbu/file1.sh ~"$username" command and execute it directly in the script, it works.



I guess the problem is that ~ gets expanded in the bash script, but can't figure out how to solve this problem.



Thank you in advance.









share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 17:58









ilkkachu

49.2k672136




49.2k672136










asked Mar 7 at 17:42









Ignas Poška

132




132







  • 1




    Just a note, ~ is tilde, and $username is a variable; there are no "wildcards" present here. ... Unless one shows up to answer :)
    – Jeff Schaller
    Mar 7 at 17:52










  • Relating only: unix.stackexchange.com/questions/270274/…
    – Jeff Schaller
    Mar 7 at 17:56












  • 1




    Just a note, ~ is tilde, and $username is a variable; there are no "wildcards" present here. ... Unless one shows up to answer :)
    – Jeff Schaller
    Mar 7 at 17:52










  • Relating only: unix.stackexchange.com/questions/270274/…
    – Jeff Schaller
    Mar 7 at 17:56







1




1




Just a note, ~ is tilde, and $username is a variable; there are no "wildcards" present here. ... Unless one shows up to answer :)
– Jeff Schaller
Mar 7 at 17:52




Just a note, ~ is tilde, and $username is a variable; there are no "wildcards" present here. ... Unless one shows up to answer :)
– Jeff Schaller
Mar 7 at 17:52












Relating only: unix.stackexchange.com/questions/270274/…
– Jeff Schaller
Mar 7 at 17:56




Relating only: unix.stackexchange.com/questions/270274/…
– Jeff Schaller
Mar 7 at 17:56










4 Answers
4






active

oldest

votes

















up vote
3
down vote



accepted










You seem on the ball so I won't spoon-feed you a script, but here are some pointers:




  1. You're using awk incorrectly. Try this instead:



    awk -F: -v uid_min=$UID_MIN:-1000 '$3%2==0 && $3>uid_min && $3!=65534print $6' /etc/passwd


  2. There's no need for cat.


  3. Use your awk output as the input to your read loop, ie. while read USER_HOME_DIR ; do ... ; done < $(awk...). Understand that this will mean that only one awk process needs to be spawned, while your original script spawns a separate awk process for each line, so this is much more efficient.


  4. Add a check in your awk program to limit it to UID's above 1000, or you will inadvertently perform your copy for many system users.


  5. Note that in #1 above, I changed your $1 to $6 in order to pull the user's home directory instead of the user's name.


  6. I just noticed on my debian machine that there exists a 'nobody' user with UID 65534, so you may need to account for that. I modified the awk statement in #1 accordingly.


  7. As per user Jeff Schaller's comment, I've modified the awk program to account for a custom minimum UID. The form $UID_MIN:-1000 means set the value as 1000 if it would otherwise be null or unset.






share|improve this answer


















  • 1




    The Q isn't tagged linux, but if that's the scope, you might use UID_MIN rather than hard-coding 1000 in case the local sysadmin adjusted it.
    – Jeff Schaller
    Mar 7 at 18:12










  • @JeffSchaller - Thanks. incorporated into the answer.
    – user1404316
    Mar 7 at 18:32










  • I should have clarified -- UID_MIN isn't globally set; it's a parameter in /etc/login.defs; sorry!
    – Jeff Schaller
    Mar 7 at 18:39










  • Well, at least it was an opportunity teach the shell idiom $FOO:-default. At this point, I'll leave it in for that reason, and won't further change the answer, on the basis that the person running the script or one-liner will be a sysadmin who will know what the UID_MIN value is.
    – user1404316
    Mar 7 at 18:46

















up vote
7
down vote













The tilde expansion doesn't work if the username part is quoted:



$ echo ~root ~"root"
/root ~root


(and it happens before variable expansion anyway.)



But since you're already reading passwd in the shell, why not just do it the full way:



#!/bin/bash
getent passwd | while IFS=: read -r user pass uid gid gecos home shell; do
if (( uid % 2 == 0 )); then
echo "uid $uid of user '$user' is even and their home is '$home'";
fi;
done


If your system has getent, it's probably better to use it, rather than /etc/passwd directly.



On the other hand, since you don't really seem to need the username for anything, you could just have your awk script output the directory ($6) instead.






share|improve this answer



























    up vote
    6
    down vote













    That's because tilde expansion happens before variable expansions:




    The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and filename expansion.




    echoing the result looks correct, and works if you re-evaluate it, but for a script, you'll need to do it differently. Perhaps:



    # ...
    homedir=$(getent passwd "$username" | cut -d: -f6)
    cp /root/nbu/file1.sh "$homedir"
    # ...





    share|improve this answer



























      up vote
      1
      down vote













      I think I can do it in a one-liner. Check this out:



      eval $(awk -v source="/root/nbu/file1.sh" -v uid_min=$UID_MIN:-1000 -F: '$3%2==0 && $3>uid_min && $3!=65534printf "cp %s %s ;", source, $6 ' /etc/passwd)


      For readability:



      eval $( 
      awk
      -v source="/root/nbu/file1.sh"
      -v uid_min=$UID_MIN:-1000
      -F: '
      $3%2==0 && $3>uid_min && $3!=65534
      printf "cp %s %s ;", source, $6

      ' /etc/passwd
      )





      share|improve this answer


















      • 1




        I would use source <( awk '...' ) instead of eval.
        – glenn jackman
        Mar 7 at 19:17










      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%2f428811%2fbash-script-copy-file-to-users-wildcard-home-dir%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote



      accepted










      You seem on the ball so I won't spoon-feed you a script, but here are some pointers:




      1. You're using awk incorrectly. Try this instead:



        awk -F: -v uid_min=$UID_MIN:-1000 '$3%2==0 && $3>uid_min && $3!=65534print $6' /etc/passwd


      2. There's no need for cat.


      3. Use your awk output as the input to your read loop, ie. while read USER_HOME_DIR ; do ... ; done < $(awk...). Understand that this will mean that only one awk process needs to be spawned, while your original script spawns a separate awk process for each line, so this is much more efficient.


      4. Add a check in your awk program to limit it to UID's above 1000, or you will inadvertently perform your copy for many system users.


      5. Note that in #1 above, I changed your $1 to $6 in order to pull the user's home directory instead of the user's name.


      6. I just noticed on my debian machine that there exists a 'nobody' user with UID 65534, so you may need to account for that. I modified the awk statement in #1 accordingly.


      7. As per user Jeff Schaller's comment, I've modified the awk program to account for a custom minimum UID. The form $UID_MIN:-1000 means set the value as 1000 if it would otherwise be null or unset.






      share|improve this answer


















      • 1




        The Q isn't tagged linux, but if that's the scope, you might use UID_MIN rather than hard-coding 1000 in case the local sysadmin adjusted it.
        – Jeff Schaller
        Mar 7 at 18:12










      • @JeffSchaller - Thanks. incorporated into the answer.
        – user1404316
        Mar 7 at 18:32










      • I should have clarified -- UID_MIN isn't globally set; it's a parameter in /etc/login.defs; sorry!
        – Jeff Schaller
        Mar 7 at 18:39










      • Well, at least it was an opportunity teach the shell idiom $FOO:-default. At this point, I'll leave it in for that reason, and won't further change the answer, on the basis that the person running the script or one-liner will be a sysadmin who will know what the UID_MIN value is.
        – user1404316
        Mar 7 at 18:46














      up vote
      3
      down vote



      accepted










      You seem on the ball so I won't spoon-feed you a script, but here are some pointers:




      1. You're using awk incorrectly. Try this instead:



        awk -F: -v uid_min=$UID_MIN:-1000 '$3%2==0 && $3>uid_min && $3!=65534print $6' /etc/passwd


      2. There's no need for cat.


      3. Use your awk output as the input to your read loop, ie. while read USER_HOME_DIR ; do ... ; done < $(awk...). Understand that this will mean that only one awk process needs to be spawned, while your original script spawns a separate awk process for each line, so this is much more efficient.


      4. Add a check in your awk program to limit it to UID's above 1000, or you will inadvertently perform your copy for many system users.


      5. Note that in #1 above, I changed your $1 to $6 in order to pull the user's home directory instead of the user's name.


      6. I just noticed on my debian machine that there exists a 'nobody' user with UID 65534, so you may need to account for that. I modified the awk statement in #1 accordingly.


      7. As per user Jeff Schaller's comment, I've modified the awk program to account for a custom minimum UID. The form $UID_MIN:-1000 means set the value as 1000 if it would otherwise be null or unset.






      share|improve this answer


















      • 1




        The Q isn't tagged linux, but if that's the scope, you might use UID_MIN rather than hard-coding 1000 in case the local sysadmin adjusted it.
        – Jeff Schaller
        Mar 7 at 18:12










      • @JeffSchaller - Thanks. incorporated into the answer.
        – user1404316
        Mar 7 at 18:32










      • I should have clarified -- UID_MIN isn't globally set; it's a parameter in /etc/login.defs; sorry!
        – Jeff Schaller
        Mar 7 at 18:39










      • Well, at least it was an opportunity teach the shell idiom $FOO:-default. At this point, I'll leave it in for that reason, and won't further change the answer, on the basis that the person running the script or one-liner will be a sysadmin who will know what the UID_MIN value is.
        – user1404316
        Mar 7 at 18:46












      up vote
      3
      down vote



      accepted







      up vote
      3
      down vote



      accepted






      You seem on the ball so I won't spoon-feed you a script, but here are some pointers:




      1. You're using awk incorrectly. Try this instead:



        awk -F: -v uid_min=$UID_MIN:-1000 '$3%2==0 && $3>uid_min && $3!=65534print $6' /etc/passwd


      2. There's no need for cat.


      3. Use your awk output as the input to your read loop, ie. while read USER_HOME_DIR ; do ... ; done < $(awk...). Understand that this will mean that only one awk process needs to be spawned, while your original script spawns a separate awk process for each line, so this is much more efficient.


      4. Add a check in your awk program to limit it to UID's above 1000, or you will inadvertently perform your copy for many system users.


      5. Note that in #1 above, I changed your $1 to $6 in order to pull the user's home directory instead of the user's name.


      6. I just noticed on my debian machine that there exists a 'nobody' user with UID 65534, so you may need to account for that. I modified the awk statement in #1 accordingly.


      7. As per user Jeff Schaller's comment, I've modified the awk program to account for a custom minimum UID. The form $UID_MIN:-1000 means set the value as 1000 if it would otherwise be null or unset.






      share|improve this answer














      You seem on the ball so I won't spoon-feed you a script, but here are some pointers:




      1. You're using awk incorrectly. Try this instead:



        awk -F: -v uid_min=$UID_MIN:-1000 '$3%2==0 && $3>uid_min && $3!=65534print $6' /etc/passwd


      2. There's no need for cat.


      3. Use your awk output as the input to your read loop, ie. while read USER_HOME_DIR ; do ... ; done < $(awk...). Understand that this will mean that only one awk process needs to be spawned, while your original script spawns a separate awk process for each line, so this is much more efficient.


      4. Add a check in your awk program to limit it to UID's above 1000, or you will inadvertently perform your copy for many system users.


      5. Note that in #1 above, I changed your $1 to $6 in order to pull the user's home directory instead of the user's name.


      6. I just noticed on my debian machine that there exists a 'nobody' user with UID 65534, so you may need to account for that. I modified the awk statement in #1 accordingly.


      7. As per user Jeff Schaller's comment, I've modified the awk program to account for a custom minimum UID. The form $UID_MIN:-1000 means set the value as 1000 if it would otherwise be null or unset.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Mar 7 at 21:39

























      answered Mar 7 at 18:01









      user1404316

      2,314520




      2,314520







      • 1




        The Q isn't tagged linux, but if that's the scope, you might use UID_MIN rather than hard-coding 1000 in case the local sysadmin adjusted it.
        – Jeff Schaller
        Mar 7 at 18:12










      • @JeffSchaller - Thanks. incorporated into the answer.
        – user1404316
        Mar 7 at 18:32










      • I should have clarified -- UID_MIN isn't globally set; it's a parameter in /etc/login.defs; sorry!
        – Jeff Schaller
        Mar 7 at 18:39










      • Well, at least it was an opportunity teach the shell idiom $FOO:-default. At this point, I'll leave it in for that reason, and won't further change the answer, on the basis that the person running the script or one-liner will be a sysadmin who will know what the UID_MIN value is.
        – user1404316
        Mar 7 at 18:46












      • 1




        The Q isn't tagged linux, but if that's the scope, you might use UID_MIN rather than hard-coding 1000 in case the local sysadmin adjusted it.
        – Jeff Schaller
        Mar 7 at 18:12










      • @JeffSchaller - Thanks. incorporated into the answer.
        – user1404316
        Mar 7 at 18:32










      • I should have clarified -- UID_MIN isn't globally set; it's a parameter in /etc/login.defs; sorry!
        – Jeff Schaller
        Mar 7 at 18:39










      • Well, at least it was an opportunity teach the shell idiom $FOO:-default. At this point, I'll leave it in for that reason, and won't further change the answer, on the basis that the person running the script or one-liner will be a sysadmin who will know what the UID_MIN value is.
        – user1404316
        Mar 7 at 18:46







      1




      1




      The Q isn't tagged linux, but if that's the scope, you might use UID_MIN rather than hard-coding 1000 in case the local sysadmin adjusted it.
      – Jeff Schaller
      Mar 7 at 18:12




      The Q isn't tagged linux, but if that's the scope, you might use UID_MIN rather than hard-coding 1000 in case the local sysadmin adjusted it.
      – Jeff Schaller
      Mar 7 at 18:12












      @JeffSchaller - Thanks. incorporated into the answer.
      – user1404316
      Mar 7 at 18:32




      @JeffSchaller - Thanks. incorporated into the answer.
      – user1404316
      Mar 7 at 18:32












      I should have clarified -- UID_MIN isn't globally set; it's a parameter in /etc/login.defs; sorry!
      – Jeff Schaller
      Mar 7 at 18:39




      I should have clarified -- UID_MIN isn't globally set; it's a parameter in /etc/login.defs; sorry!
      – Jeff Schaller
      Mar 7 at 18:39












      Well, at least it was an opportunity teach the shell idiom $FOO:-default. At this point, I'll leave it in for that reason, and won't further change the answer, on the basis that the person running the script or one-liner will be a sysadmin who will know what the UID_MIN value is.
      – user1404316
      Mar 7 at 18:46




      Well, at least it was an opportunity teach the shell idiom $FOO:-default. At this point, I'll leave it in for that reason, and won't further change the answer, on the basis that the person running the script or one-liner will be a sysadmin who will know what the UID_MIN value is.
      – user1404316
      Mar 7 at 18:46












      up vote
      7
      down vote













      The tilde expansion doesn't work if the username part is quoted:



      $ echo ~root ~"root"
      /root ~root


      (and it happens before variable expansion anyway.)



      But since you're already reading passwd in the shell, why not just do it the full way:



      #!/bin/bash
      getent passwd | while IFS=: read -r user pass uid gid gecos home shell; do
      if (( uid % 2 == 0 )); then
      echo "uid $uid of user '$user' is even and their home is '$home'";
      fi;
      done


      If your system has getent, it's probably better to use it, rather than /etc/passwd directly.



      On the other hand, since you don't really seem to need the username for anything, you could just have your awk script output the directory ($6) instead.






      share|improve this answer
























        up vote
        7
        down vote













        The tilde expansion doesn't work if the username part is quoted:



        $ echo ~root ~"root"
        /root ~root


        (and it happens before variable expansion anyway.)



        But since you're already reading passwd in the shell, why not just do it the full way:



        #!/bin/bash
        getent passwd | while IFS=: read -r user pass uid gid gecos home shell; do
        if (( uid % 2 == 0 )); then
        echo "uid $uid of user '$user' is even and their home is '$home'";
        fi;
        done


        If your system has getent, it's probably better to use it, rather than /etc/passwd directly.



        On the other hand, since you don't really seem to need the username for anything, you could just have your awk script output the directory ($6) instead.






        share|improve this answer






















          up vote
          7
          down vote










          up vote
          7
          down vote









          The tilde expansion doesn't work if the username part is quoted:



          $ echo ~root ~"root"
          /root ~root


          (and it happens before variable expansion anyway.)



          But since you're already reading passwd in the shell, why not just do it the full way:



          #!/bin/bash
          getent passwd | while IFS=: read -r user pass uid gid gecos home shell; do
          if (( uid % 2 == 0 )); then
          echo "uid $uid of user '$user' is even and their home is '$home'";
          fi;
          done


          If your system has getent, it's probably better to use it, rather than /etc/passwd directly.



          On the other hand, since you don't really seem to need the username for anything, you could just have your awk script output the directory ($6) instead.






          share|improve this answer












          The tilde expansion doesn't work if the username part is quoted:



          $ echo ~root ~"root"
          /root ~root


          (and it happens before variable expansion anyway.)



          But since you're already reading passwd in the shell, why not just do it the full way:



          #!/bin/bash
          getent passwd | while IFS=: read -r user pass uid gid gecos home shell; do
          if (( uid % 2 == 0 )); then
          echo "uid $uid of user '$user' is even and their home is '$home'";
          fi;
          done


          If your system has getent, it's probably better to use it, rather than /etc/passwd directly.



          On the other hand, since you don't really seem to need the username for anything, you could just have your awk script output the directory ($6) instead.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 17:55









          ilkkachu

          49.2k672136




          49.2k672136




















              up vote
              6
              down vote













              That's because tilde expansion happens before variable expansions:




              The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and filename expansion.




              echoing the result looks correct, and works if you re-evaluate it, but for a script, you'll need to do it differently. Perhaps:



              # ...
              homedir=$(getent passwd "$username" | cut -d: -f6)
              cp /root/nbu/file1.sh "$homedir"
              # ...





              share|improve this answer
























                up vote
                6
                down vote













                That's because tilde expansion happens before variable expansions:




                The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and filename expansion.




                echoing the result looks correct, and works if you re-evaluate it, but for a script, you'll need to do it differently. Perhaps:



                # ...
                homedir=$(getent passwd "$username" | cut -d: -f6)
                cp /root/nbu/file1.sh "$homedir"
                # ...





                share|improve this answer






















                  up vote
                  6
                  down vote










                  up vote
                  6
                  down vote









                  That's because tilde expansion happens before variable expansions:




                  The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and filename expansion.




                  echoing the result looks correct, and works if you re-evaluate it, but for a script, you'll need to do it differently. Perhaps:



                  # ...
                  homedir=$(getent passwd "$username" | cut -d: -f6)
                  cp /root/nbu/file1.sh "$homedir"
                  # ...





                  share|improve this answer












                  That's because tilde expansion happens before variable expansions:




                  The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and filename expansion.




                  echoing the result looks correct, and works if you re-evaluate it, but for a script, you'll need to do it differently. Perhaps:



                  # ...
                  homedir=$(getent passwd "$username" | cut -d: -f6)
                  cp /root/nbu/file1.sh "$homedir"
                  # ...






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 7 at 17:51









                  Jeff Schaller

                  31.2k846105




                  31.2k846105




















                      up vote
                      1
                      down vote













                      I think I can do it in a one-liner. Check this out:



                      eval $(awk -v source="/root/nbu/file1.sh" -v uid_min=$UID_MIN:-1000 -F: '$3%2==0 && $3>uid_min && $3!=65534printf "cp %s %s ;", source, $6 ' /etc/passwd)


                      For readability:



                      eval $( 
                      awk
                      -v source="/root/nbu/file1.sh"
                      -v uid_min=$UID_MIN:-1000
                      -F: '
                      $3%2==0 && $3>uid_min && $3!=65534
                      printf "cp %s %s ;", source, $6

                      ' /etc/passwd
                      )





                      share|improve this answer


















                      • 1




                        I would use source <( awk '...' ) instead of eval.
                        – glenn jackman
                        Mar 7 at 19:17














                      up vote
                      1
                      down vote













                      I think I can do it in a one-liner. Check this out:



                      eval $(awk -v source="/root/nbu/file1.sh" -v uid_min=$UID_MIN:-1000 -F: '$3%2==0 && $3>uid_min && $3!=65534printf "cp %s %s ;", source, $6 ' /etc/passwd)


                      For readability:



                      eval $( 
                      awk
                      -v source="/root/nbu/file1.sh"
                      -v uid_min=$UID_MIN:-1000
                      -F: '
                      $3%2==0 && $3>uid_min && $3!=65534
                      printf "cp %s %s ;", source, $6

                      ' /etc/passwd
                      )





                      share|improve this answer


















                      • 1




                        I would use source <( awk '...' ) instead of eval.
                        – glenn jackman
                        Mar 7 at 19:17












                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      I think I can do it in a one-liner. Check this out:



                      eval $(awk -v source="/root/nbu/file1.sh" -v uid_min=$UID_MIN:-1000 -F: '$3%2==0 && $3>uid_min && $3!=65534printf "cp %s %s ;", source, $6 ' /etc/passwd)


                      For readability:



                      eval $( 
                      awk
                      -v source="/root/nbu/file1.sh"
                      -v uid_min=$UID_MIN:-1000
                      -F: '
                      $3%2==0 && $3>uid_min && $3!=65534
                      printf "cp %s %s ;", source, $6

                      ' /etc/passwd
                      )





                      share|improve this answer














                      I think I can do it in a one-liner. Check this out:



                      eval $(awk -v source="/root/nbu/file1.sh" -v uid_min=$UID_MIN:-1000 -F: '$3%2==0 && $3>uid_min && $3!=65534printf "cp %s %s ;", source, $6 ' /etc/passwd)


                      For readability:



                      eval $( 
                      awk
                      -v source="/root/nbu/file1.sh"
                      -v uid_min=$UID_MIN:-1000
                      -F: '
                      $3%2==0 && $3>uid_min && $3!=65534
                      printf "cp %s %s ;", source, $6

                      ' /etc/passwd
                      )






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 7 at 19:27

























                      answered Mar 7 at 18:24









                      user1404316

                      2,314520




                      2,314520







                      • 1




                        I would use source <( awk '...' ) instead of eval.
                        – glenn jackman
                        Mar 7 at 19:17












                      • 1




                        I would use source <( awk '...' ) instead of eval.
                        – glenn jackman
                        Mar 7 at 19:17







                      1




                      1




                      I would use source <( awk '...' ) instead of eval.
                      – glenn jackman
                      Mar 7 at 19:17




                      I would use source <( awk '...' ) instead of eval.
                      – glenn jackman
                      Mar 7 at 19:17












                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428811%2fbash-script-copy-file-to-users-wildcard-home-dir%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)