0

I have a bash script in which I am ssh-ing into a server to get a specific file name, then using scp to copy that file from the remote server to my local machine. The scp command works fine when the file name is hardcoded (obviously not possible in the final script) but fails when accessing the file name via a variable. I get the file name with the following code:

for file in `ssh -t -t user@host 'cd /path/to/files && ls -t rsync* | head -1'`
do 
    FILENAME=$file
done

echo $filename works fine and echos the name of the file, in this case rsync.2016.08.31-08.42.19.log . Note there are no spaces or unusual characters in the name of the file, so this does not appear to be an issue. The file name will always take this format (only the date and time will change, which is why I felt okay with the not-recommended approach of parsing the ls command).

When I try to use the following command:

scp user@host:/path/to/files/$FILENAME .

I get the following error:

: No such file or directorync.2016.08.31-08.42.19.log

Note that the error message mashes into the file name, cutting out the first three letters. Interestingly, if I remove the /path/to/files/ from the command, I get:

: No such file or directory.19.log

(i.e., an extra 17 or so letters missing from the variable name). I tried using scp both inside the for loop (including using the $file variable in the loop instead of assigning it to $FILENAME) and outside the for loop. I tried creating a variable that includes the full path and using that instead. I tried putting brackets around the variable name. I tried using the a different path on my local machine, both full and relative. I have tried putting quotes, both double and single, around $FILENAME. It all results in the same error. The only thing that seems to work is hardcoding the file name into the command. Something about the variable seems to be the issue, but after exhausting both my brain and online resources, I am at a loss.

ce_nort
  • 168
  • 1
  • 16

0 Answers0