1

Currently I'm having an issue to pass a script with arguments to the bash. The limitation is that I can pass them as a string (see $COMMAND)

ssh user@server 'bash -s' < "$COMMAND"

if my $COMMAND contains "foo.sh bar baz", I would get error no such file or directory: foo.sh bar baz

Additionally, I'm be able to set additional options to bash.

Thank you in advance!!!

2 Answers 2

3

The root problem is that < takes a filename to redirect from, not a filename and some arguments. Mixing the two together this way just won't work.

If I'm understanding right, you want to run a script that exists on the local computer, but have it execute (with the supplied arguments) on the remote computer. In that case, what you want to run is something like this:

ssh user@server 'bash -s bar baz' <"foo.sh"

Note that the arguments are passed in a completely different place than the script filename. If possible, I'd recommend never mixing the two; keep them in separate variables, and then use something like this:

ssh user@server "bash -s $scriptargs" <"$scriptfile"
Sign up to request clarification or add additional context in comments.

1 Comment

Hey stackoverflow.com/users/89817/gordon-davisson, thanks for having a look here. Thanks for the extensive explanation. The intention was to run local script and passing local parameters to remote server. Never mind, since this is not possible either way I have to look for some other approach.
0

From the sounds of the error, foo.sh doesn't exist on server, so it fails.

The simplest solution seems to be:

ssh user@server 'foo.sh bar baz', provided "foo.sh" exists on the server.

If "foo.sh" doesn't/can't exist on the server, have you tried using here docs? For example:

ssh user@server "cat > foo.sh && chmod u+x foo.sh && ./foo.sh bar baz" << "EOF"
`heredoc> #Put your contents of foo.sh here
`heredoc> #And put them here
`heredoc> echo "Argument 1: $1"
`heredoc> echo "Argument 2: $2"
`heredoc> EOF
user@server's password:
Argument 1: bar
Argument 2: baz

EDIT:

david@localhost ~ % bash -s < test.sh David Finder
Hello David
Hello Finder

Edit to my previous answer, the issue is due to the quoting of the input arguments thrown to Bash, as it sees the entire input as one file (it's looking for a file literally called "foo.sh bar baz", with spaces within.

2 Comments

hi stackoverflow.com/users/11186403/david-allewell. Thanks for the reply. It's not problem with ssh. You can try it just with bash, by having foo.sh script and executing bash -s < "foo.sh bar baz" You will get the error.
thanks a lot for your help. Yes that would work, but remember my command is wrapped with quotes "$COMMAND". I'm wondering if I could explicitly mark the parameters. Something like "foo.sh arg_1=bar arg_2=baz"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.