0

I'm trying to pass along variables via ssh, and wrote two short testscripts (bash)

This one is to the execute the script on the other side (and it works, at least partially)

I start it with executing: 'mms test alpha one'

#!/bin/bash
    sshpass -p (password) ssh hellfire@192.168.0.11 'bash /scripts/mms2 "$@"'

The second script that are executed is:

#!/bin/bash
echo "$@" >/scripts/test1.txt

This script is only for testing if the parameters are are transfered.

So far it creates the text file, but it's empty, so I have no idea if there's wrong with both or only one of the script.

Basically I want to pass a set of variables to the script on the server, these variables can contain spaces.

Anybody have any tips?

JoBe
  • 407
  • 2
  • 14
  • Important distinction: you can't pass variables. All you can do is *locally* construct a command to pass to the remote shell. – chepner Feb 23 '20 at 18:19
  • So I can't tell it to run: 'test.sh one two three" ? I can only tell it to run the test.sh? – JoBe Feb 23 '20 at 18:26
  • @JoBe You can run something like `test.sh one two three`, because that's just a command (or if you prefer, a command name and parameters to it). You can't pass a variable, but you can expand a variable to get its value and pass *that* as an argument. But since you put `$@` in single-quotes, it's not expanded by the local shell; it's passed literally to the remote shell. – Gordon Davisson Feb 23 '20 at 18:35
  • Well, this particular script (the local one) are going to get the parameters from the program that's calling it. So it will be executed as: "mms.sh username movie title with spaces" how can I get them to the remote server? – JoBe Feb 23 '20 at 18:49
  • 1
    Use the answer to ["How do I pass arbitrary arguments to a command executed over SSH?"](https://stackoverflow.com/questions/51547753/how-do-i-pass-arbitrary-arguments-to-a-command-executed-over-ssh) (or if the remote shell might not be bash, see ["How to have simple and double quotes in a scripted ssh command"](https://stackoverflow.com/questions/45308395/how-to-have-simple-and-double-quotes-in-a-scripted-ssh-command) instead). – Gordon Davisson Feb 23 '20 at 19:08

1 Answers1

1

I found out by @Gordon Davidsson comment that the $(printf "%q " "$@") could be used to send it as a string, so the remote server didn't interpret the variables as different commands.

My new and working script is:

#!/bin/bash
sshpass -p (password) ssh hellfire@192.168.0.11 "bash /scripts/mms2 $(printf "%q " "$@")"
JoBe
  • 407
  • 2
  • 14