Shell Script: Read line in file

19,378

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"
Share:
19,378
user3228279
Author by

user3228279

Updated on June 04, 2022

Comments

  • user3228279
    user3228279 almost 2 years

    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"
    
  • Admin
    Admin about 10 years
    Seems like $ is missing in line : cp -r "var" "car" AND I don't get correct values using 'while read -d .... "