How do you run a command for each line of a file?

前端 未结 9 1495
梦谈多话
梦谈多话 2020-11-27 10:30

For example, right now I\'m using the following to change a couple of files whose Unix paths I wrote to a file:

cat file.txt | while read in; do chmod 755 \"         


        
相关标签:
9条回答
  • 2020-11-27 11:04

    The logic applies to many other objectives. And how to read .sh_history of each user from /home/ filesystem? What if there are thousand of them?

    #!/bin/ksh
    last |head -10|awk '{print $1}'|
     while IFS= read -r line
     do
    su - "$line" -c 'tail .sh_history'
     done
    

    Here is the script https://github.com/imvieira/SysAdmin_DevOps_Scripts/blob/master/get_and_run.sh

    0 讨论(0)
  • 2020-11-27 11:05

    If you know you don't have any whitespace in the input:

    xargs chmod 755 < file.txt
    

    If there might be whitespace in the paths, and if you have GNU xargs:

    tr '\n' '\0' < file.txt | xargs -0 chmod 755
    
    0 讨论(0)
  • 2020-11-27 11:07

    Yes.

    while read in; do chmod 755 "$in"; done < file.txt
    

    This way you can avoid a cat process.

    cat is almost always bad for a purpose such as this. You can read more about Useless Use of Cat.

    0 讨论(0)
提交回复
热议问题