Bash – How to run local (non-shell) scripts on a remote host via SSH

bashcatssh

How can I make cat someFile | ssh someHost work when someFile is not a bash script? I want to remotely execute a perl script but I get a bunch of syntax errors from bash when I try the cat | ssh command.

Best Answer

If you want to push the Perl script through the SSH connection, you'll have to run the Perl interpreter on the remote end. It'll read the script from stdin:

ssh remotehost perl < somescript.pl

In the case of Perl, it should even read the command line switches (except -T) from the hashbang line of the input.

If you want to give command line arguments to the Perl interpreter, you can just add them to the command line after perl. If you want to give arguments to the script, you'll need to explicitly tell the interpreter to read the script from stdin (otherwise it will take the first argument as a file name to look for).

So, here -l goes to the interpreter, and foo and bar to the script:

echo 'print "> $_"  foreach @ARGV' | ssh remotehost perl -l - foo bar 

Note that doing just ssh somehost < script.sh counts on the remote login shell being compatible with the script. (i.e. a Bash script won't work if the remote shell happens to be something else.)

Related Question