Shell Script: Read line in file

删除回忆录丶 提交于 2019-12-24 05:03:03

问题


I have a file paths.txt:

/my/path/Origin/.:your/path/Destiny/.
/my/path/Origin2/.:your/path/Destiny2/.
/...
/...

I need a Script CopyPaste.sh using file paths.txt to copy all files in OriginX to DestinyX

Something like that:

 #!/bin/sh

while read line
do
        var= $line | cut --d=":" -f1
        car= $line | cut --d=":" -f2
        cp -r var car

done < "paths.txt"

回答1:


Use translate : tr command & apply cp command in the same go!

#!/bin/sh

while read line; do
  cp `echo $line | tr ':' ' '`
done < "paths.txt"



回答2:


You need to use command substitution to get command's output into a shell variable:

#!/bin/sh

while read line
do
        var=`echo $line | cut --d=":" -f1`
        car=`echo $line | cut --d=":" -f2`
        cp -r "$var" "$car"
done < "paths.txt"

Though your script can be simplified using read -d:

while read -d ":" var car; do
  cp -r "$var" "$car"
done < "paths.txt"


来源:https://stackoverflow.com/questions/21478334/shell-script-read-line-in-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!