Okay, so I've been struggling with this one for awhile. I'm trying to create a python3 script that automatically uploads a file to a server. In the commandline, the following command works like a charm:
sftp -i key.pem -P 3912 admin@myServerIP:home/files/ <<< $"put test.txt"
But, when I try to run that command in my python script (either through os.system('command')
or subprocess.call('command')
) I keep getting the following error:
/bin/sh: 1: Syntax error: redirection unexpected
After doing some research, I believe I found that I need to run the command in /bin/bash, instead of the default dash, but I've tried everything, and connot for the life of me figure out how to do that! I hope this is a simple, stupid fix, so I can be on my way. Thanks!
You can replace your command with this one:
/bin/bash -c 'sftp -i key.pem -P 3912 admin@myServerIP:home/files/ <<< $"put test.txt"'
which should work fine even in Dash. :-)
(Incidentally, I think you meant to write "put test.txt"
instead of $"put test.txt"
. But that's neither here nor there.)
Edited to add: I should mention that your specific example is easy to rewrite in a Dash-compatible way:
echo "put test.txt" | sftp -i key.pem -P 3912 admin@myServerIP:home/files/
which may make more sense in this case. But you will very likely find the bash -c ...
approach useful for other things in the future.