Search code examples
bashmacosscp

Why isn't my while loop working through scp when reiterating through a list


Hi I am trying to transfer a list of files from the server to my computer using a while loop, but it keeps giving me the error that there is no file or directory. I can't understand why.

So I have this file list_numbers.txt with over 500 lines

list_numbers.txt
1234
345
2135
2132
...

And I want to re-iterate through this list to transfer files from server to my computer with the corresponding foldername to my computer but replacing assembly_graph.txt with $line_for_assembly.txt

while read line; do scp -r user@server.com:/home/Documents/$line_assembly/assembly_graph.txt /Users/Documents/$line_for_assembly.txt; done < list_numbers.txt

So basically I want it to do the below code, but instead of me having to manually type it, it would re-iterate through the list.

scp -r user@server.com:/home/Documents/1234_assembly/assembly_graph.txt /Users/Documents/1234_for_assembly.txt

scp -r user@server.com:/home/Documents/345_assembly/assembly_graph.txt /Users/Documents/345_for_assembly.txt

scp -r user@server.com:/home/Documents/2135_assembly/assembly_graph.txt /Users/Documents/2135_for_assembly.txt

scp -r user@server.com:/home/Documents/2132_assembly/assembly_graph.txt /Users/Documents/2132_for_assembly.txt

But I get this error:

scp: /home/Documents//assembly_graph.txt: No such file or directory

So it is not picking up the $line_assembly, even when I do ${line}_assembly. I don't know what I'm doing wrong.


Solution

  • because you do not have a variable named line_assembly.

    Perhaps you mean to the following:

    while read line
     do
      line=`echo $line | tr -d '\r\n'`
       scp -r user@server.com:/home/Documents/${line}_assembly/assembly_graph.txt /Users/Documents/${line}_for_assembly.txt
    done < list_numbers.txt
    

    this will expand as the value of line followed by the text "_assembly".

    For example if "line" has the value "component" you will get

    .../Documents/component_assembly/...

    EDIT: OP had spurious characters in his/her input file (probably \r characters from windows). I've edited the answer accordingly to accommodate OP's specific issue.