To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command

> ssh -qtt user@host < test.sh

However, when I try to pass arguments to test.sh it fails.
Any pointers would be appreciated.

Try

> ssh -qtt user@host < 'test.sh YourArgHere' 

and let us know how it went.

I tried ssh -qtt user@host < 'test.sh 00'
I get an error

test.sh 00: No such file or directory

I don't think that will work. Redirection opens a file and feeds its contents to stdin. Arguments are expanded by the shell and supplied to the script as positional parameters. Don't forget the shell runs on the remote host!

Any pointers on how I can pass positional parameters to the local script executing on remote machine.

You are right; my bad.
What I'd do then, is use here documents to supply the ssh connection with a series of commands. I know for a fact that that indeed works. See this link, and take a look at examples 19-5 and 19-6.

Try this one:

ssh -qtt user@host 'sh -s arg1 arg2' < test.sh 

and report back if it works...!

2 Likes

Hurray it works! Thanks a lot! Please can you let me know why the parameters need to be passed with sh.

ssh -qtt user@host 'sh -s 00' < test.sh

works fine, but if I pass a variable it doesnt resolve the value
Eg:

[iCODE]ssh -qtt user@host 'sh -s "$version"' < test.sh[i/CODE] goes in as $version only.

I got the ssh command to resolve the variable value.

ssh -qtt ${user}@${host} "sh -s "${version}""  <  test.sh 

Thanks folks!