简体   繁体   中英

BASH - Filename with space passed to sFTP not resolving peroperly

I'm trying to pass a filename with spaces to a script that will download the passed filename from an sFTP server. I'm having an issue with the script recognizing that, after resolving, is one filename instead of two words.

export tloc=/path/to/target
export usr=username
export svr=host.name.com
export pwd=pass1234
export sloc=/path/to/source
export file="test file_12345.zip"

#-- Perform download of file specified using passed paramters.
sshpass -p $pwd sftp -o StrictHostKeyChecking=no $usr@$svr:$sloc/"$file" $tloc

I understand that when the script executes, it will resolve the words contained in the quotes as one word and then remove the quotes. What am I missing here? In other languages, I've been able to nest quotes or use some other syntax to have the end result appear as below.

sshpass -p pass1234 sftp -o StrictHostKeyChecking=no usernamehost.name.com:/path/to/source/"test file_12345.zip" /path/to/target

Any help would be most appreciated.

That's due to a misfeature (bug?) of sftp. The filename you pass on the command line is effectively provided to the sftp's interactive "get" command as-is, but the get command splits on whitespace and uses the part after the space as the destination, ignoring your $tloc command line argument. You need an extra level of quoting to get it to do what you want:

sshpass -p $pwd sftp -o StrictHostKeyChecking=no $usr@$svr:$sloc/"\"$file\"" $tloc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM