How can I execute local script on remote machine and include arguments?

  • I have written a script that runs fine when executed locally:

    ./sysMole -time Aug 18 18

    The arguments "-time", "Aug", "18", and "18" are successfully passed on to the script.

    Now, this script is designed to be executed on a remote machine but, from a local directory on the local machine. Example:

    ssh [email protected] "bash -s" < /var/www/html/ops1/sysMole

    That also works fine. But the problem arises when I try to include those aforementioned arguments (-time Aug 18 18), for example:

    ssh [email protected] "bash -s" < /var/www/html/ops1/sysMole -time Aug 18 18

    After running that script I get the following error:

    bash: cannot set terminal process group (-1): Invalid argument
    bash: no job control in this shell

    Please tell me what I'm doing wrong, this greatly frustrating.

    "Bash -s" is one of the ways you can execute an script from standard input (ie. a file).

  • slm

    slm Correct answer

    7 years ago

    You were pretty close with your example. It works just fine when you use it with arguments such as these.

    Sample script:

    $ more ex.bash 
    echo $1 $2

    Example that works:

    $ ssh serverA "bash -s" < ./ex.bash "hi" "bye"
    hi bye

    But it fails for these types of arguments:

    $ ssh serverA "bash -s" < ./ex.bash "--time" "bye"
    bash: --: invalid option

    What's going on?

    The problem you're encountering is that the argument, -time, or --time in my example, is being interpreted as a switch to bash -s. You can pacify bash by terminating it from taking any of the remaining command line arguments for itself using the -- argument.

    Like this:

    $ ssh [email protected] "bash -s" -- < /var/www/html/ops1/sysMole -time Aug 18 18



    $ ssh serverA "bash -s" -- < ./ex.bash "-time" "bye"
    -time bye


    $ ssh serverA "bash -s" -- < ./ex.bash "--time" "bye"
    --time bye


    $ ssh serverA "bash -s" -- < ./ex.bash --time "bye"
    --time bye


    $ ssh  < ./ex.bash serverA "bash -s -- --time bye"
    --time bye

    NOTE: Just to make it clear that wherever the redirection appears on the command line makes no difference, because ssh calls a remote shell with the concatenation of its arguments anyway, quoting doesn't make much difference, except when you need quoting on the remote shell like in example #4:

    $ ssh  < ./ex.bash serverA "bash -s -- '<--time bye>' '<end>'"
    <--time bye> <end>

    you are an Genius! What do I have to do to get to your level? I have alot of work cut out. Thanks so much.

    @AllenD - just keep asking questions, and try and participate on the site as much as you can. I always try and learn something new each day. Before your question I didn't know how to do this either. 8-). Thanks for your question!

    note that the redirection can appear at any point in the command: for example `bash -s -- --time bye < ./ex.bash` or even `< ./ex.bash bash -s -- --time bye`. this is because the shell first takes out the redirection instruction (regardless of where it is in the command), then sets up the redirection, then executes the rest of the command line with the redirection in place.

    @sim I'm trying to do this exact same thing, but from within a script, not from the command line. Any idea of how to do this? For some reason, enclosing your exact answer in backticks does not work at all. It instead runs the script specified locally, and the output is sent as a command to the bash over ssh

    @krb686 - I'd ask it as a new Q.

    I knew "bash -s" but I didn't know how to stop bash from interpreting other switches which were not meant for bash. Useful tip.

    @slm thank you for the great answer. Is it possible to pass file as an argument. Say run `ssh [email protected] '' ' < file_input` . The first part would work but how to make the `` on `server` to read `file_input` on the local pc

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM