How do I pass a variable into sftp?
Solution 1
The sftp program is its own thing; it's completely independent from and unrelated to bash, which knows the value of its variables and would expand $yr. In general, you can't pass bash variables into external programs like that without application-specific communication methods.
If you just want to upload or download a file with $yr in the filename, you could use scp instead, something like:
yr=$(date +%Y)
scp -i key.pem [email protected]:/some/long/path/that/includes/$yr .
for download, or
scp -i key.pem some_local_file [email protected]:/some/long/$yr/path
for upload.
If for some reason you really require using sftp specifically, you could create a batch file dynamically, e.g.
sftp -i key.pem -b - [email protected] <<< "get /some/path/with/$yr"
Solution 2
sftp is a standalone program, not a part of the shell. It doesn't understand shell syntax. It doesn't have features such as variable expansion and conditional statements.
The easiest way to do complex things over SFTP is to use SSHFS. SSHFS is a filesystem that uses SFTP to make a remote filesystem appear as a local filessytem. On the client, SSHFS requires FUSE, which is available on most modern unices. On the server, SSHFS requires SFTP; if the server allows SFTP then you can use SSHFS with it.
mkdir server
sshfs -o IdentityFile=key.pem [email protected]: server
cp "server/dir/myfile-$(date +%Y).txt" /local/path
fusermount -u server
Related videos on Youtube
simplycoding
Updated on September 18, 2022Comments
-
simplycoding 4 monthsI'm trying to match a filename to a string, which is basically a timestamp, during an SFTP connection.
How do I match part of the filename? I can't seem to declare a variable within an SFTP connection. And neither can I call a variable created in Bash beforehand.
Edit: things I've tried
yr=$(date + "%Y") sftp -i key.pem [email protected] sftp> echo $yr Invalid command. sftp> $yr Invalid command.new shell
sftp -i key.pem [email protected] sftp> test=$(date +"%Y") Invalid command.Tried those 2
-
dhag over 7 yearsHi! It would be helpful if you edited your question to show the exact command you are using, so we can tell what you tried and what, precisely, doesn't work. -
Ed Heal over 7 yearsSFTP is a protocol. Can be used in various ways -
simplycoding over 7 yearsadded in code of what I tried -
Ed Heal over 7 yearsUseexpect- that should do the business for you -
simplycoding over 7 yearsIf I useexpect, I need to probably do a bunch ofexpectandsendlines, right? Or can I just useexpectonce for the timestamp?
-
-
simplycoding over 7 yearsYeah I looked intoscpbut it's not allowed on the server. I'm looking intoexpectat the moment -
Tom Hunt over 7 yearsexpectcan do a lot, but it's fundamentally a hack used to allow scripting of programs which aren't designed to be scriptable. If you can use a different method, e.g. the-boption to sftp, it's usually easier. (Parenthetically, how is your server set up, that sftp is allowed but scp isn't?) -
simplycoding over 7 yearsIt's not my server, but all I know at the moment is thatscpisn't allowed. Could you explain aht-bwould do? Looking at documenation, its effect is that it notifies when jobs running in background terminate -
simplycoding over 7 yearsShould I be able to set a variable in a here-doc? Or would that throw the same error as before? -
Tom Hunt over 7 yearsYou can expand variables in either heredocs or herestrings (herestring is the syntax I used above).