-1

I'm new to shell script, currently I have a shell script named deploy.sh, it can take some options like -n namespaceName -a appName and so, like the following.

./deploy.sh -n namespaceA -a appB

but when I execute it like the following via SSH, it doesn't work,

ssh root@$remoteHost  'bash -s' < deploy.sh -n namespaceA -a appB

And when I modify the script to accept paramters directly instead of options(like the above), it works fine.

ssh root@$remoteHost  'bash -s' < deploy.sh namespaceA appB

Is there any way to pass options to it? Anyone can shed some light? TIA.

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • Using [getopts](https://stackoverflow.com/questions/45744569/how-to-handle-bash-with-multiple-arguments-for-multiple-options) should do should the trick for you. Also if you put `#!/bin/bash` at the top of your script , you won't have to call `bash -s` in your command line... – Jamie_D Jan 19 '20 at 12:39
  • `ssh root@$remoteHost "./deploy.sh -n namespaceA -a appB"`? – Cyrus Jan 19 '20 at 12:51
  • @Cyrus I've tried that, it dosen't work. It prompts `bash: ./deploy.sh: No such file or directory`, it does exist in the same dir. – George Macus Jan 20 '20 at 01:22
  • @Jamie_D I AM USING the getopts command in deploy.sh file, and no, it doesn't work neither, ```#!/bin/bash ssh root@10.100.111.11 deploy.sh -n namespaceA -a appB ``` It says ` deploy.sh: command not found` – George Macus Jan 20 '20 at 01:24

1 Answers1

1

You can do this :

ssh root@$remoteHost  'bash -s' < deploy.sh -- -n namespaceA -a appB

The -- is added to avoid -n namespaceA -a appB being taken by ssh command.

Philippe
  • 20,025
  • 2
  • 23
  • 32
  • Genius, you made my day bro!!! I've seached it for a long time, had no lcue. And your comment is very clear. Thanks very much. – George Macus Jan 20 '20 at 01:30