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 un@server:/some/long/path/that/includes/$yr .
for download, or
scp -i key.pem some_local_file un@server:/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 - un@server <<< "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 un@server: server
cp "server/dir/myfile-$(date +%Y).txt" /local/path
fusermount -u server
Related videos on Youtube
simplycoding
Updated on September 18, 2022Comments
-
simplycoding over 1 year
I'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 un@server sftp> echo $yr Invalid command. sftp> $yr Invalid command.
new shell
sftp -i key.pem un@server sftp> test=$(date +"%Y") Invalid command.
Tried those 2
-
dhag over 8 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 8 yearsSFTP is a protocol. Can be used in various ways
-
simplycoding over 8 yearsadded in code of what I tried
-
Ed Heal over 8 yearsUse
expect
- that should do the business for you -
simplycoding over 8 yearsIf I use
expect
, I need to probably do a bunch ofexpect
andsend
lines, right? Or can I just useexpect
once for the timestamp?
-
-
simplycoding over 8 yearsYeah I looked into
scp
but it's not allowed on the server. I'm looking intoexpect
at the moment -
Tom Hunt over 8 years
expect
can 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-b
option to sftp, it's usually easier. (Parenthetically, how is your server set up, that sftp is allowed but scp isn't?) -
simplycoding over 8 yearsIt's not my server, but all I know at the moment is that
scp
isn't allowed. Could you explain aht-b
would do? Looking at documenation, its effect is that it notifies when jobs running in background terminate -
simplycoding over 8 yearsShould I be able to set a variable in a here-doc? Or would that throw the same error as before?
-
Tom Hunt over 8 yearsYou can expand variables in either heredocs or herestrings (herestring is the syntax I used above).