15

I would like to expand a little more on "Bash - How to pass arguments to a script that is read via standard input" post.

I would like to create a script that takes standard input and runs it remotely while passing arguments to it.

Simplified contents of the script that I'm building:

ssh server_name bash <&0

How do I take the following method of accepting arguments and apply it to my script?

cat script.sh | bash /dev/stdin arguments

Maybe I am doing this incorrectly, please provide alternate solutions as well.

2
  • The command-line args are insufficient for this application? Commented Dec 16, 2011 at 1:25
  • How do I supply command line arguments to ssh server_name bash <&0 , so that they are not interpreted as bash options? Commented Dec 16, 2011 at 1:27

3 Answers 3

19

Try this:

cat script.sh | ssh some_server bash -s - <arguments>
Sign up to request clarification or add additional context in comments.

Comments

2

ssh shouldn't make a difference:

$ cat do_x 
#!/bin/sh

arg1=$1
arg2=$2
all_cmdline=$*
read arg2_from_stdin

echo "arg1: ${arg1}"
echo "arg2: ${arg2}"
echo "all_cmdline: ${all_cmdline}"
echo "arg2_from_stdin: ${arg2_from_stdin}"

$ echo 'a b c' > some_file
$ ./do_x 1 2 3 4 5 < some_file 
arg1: 1
arg2: 2
all_cmdline: 1 2 3 4 5
arg2_from_stdin: a b c
$ ssh some-server do_x 1 2 3 4 5 < some_file
arg1: 1
arg2: 2
all_cmdline: 1 2 3 4 5
arg2_from_stdin: a b c

6 Comments

Sorry, this is not what I meant. Taking from your example ssh some-server do_x 1 2 3 4 5 < some_file, replace do_x by standard input: bash <&0 , and this line would be inside do_x script. So now, how would I pass the arguments?
I don't understand. Are you trying to write your own shell in do_x?
I am trying to create a wrapper script that runs remotely any script provided. Also I would like to support any piped input.
instead of wrapping, why not just source in the features which should be common among scripts?
may not be an answer that OP wants, but good stuff!, thanks for sharing @BrianCain . Good luck to all.
|
0

This variant on ccarton's answer also seems to work well:

ssh some_server bash -s - < script.sh <arguments>

Comments

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.