1

Regardless of why, I am trying to write a script that will let me send a command to various addresses. There is a shared key for the user, so there is no need for logging in. But this isn't working.

So, the following will not work...

#!/bin/bash
ip=$1
shift
args="'$@'"
cmd="ssh user@$ip -C $args"
output=$($cmd)

If I execute it with the following:

./myscript.sh 10.0.1.2 /bin/ls -l /var

I get the error of "ls -l /var: No such file or directory"

If I run that command (ssh user@10.0.1.2 -C '/bin/ls -l /var'), it works fine.

What am I doing wrong? These are the same installs of RHEL6.

jasonmclose
  • 1,667
  • 4
  • 22
  • 38
  • This is [BashFAQ 50](http://mywiki.wooledge.org/BashFAQ/050). You cannot put commands with quotes into strings. It does not work. More to the point you don't need to do this. Either use `"$@"` directly if you want the arguments as individual words or use `"$*"` to get the arguments as a single "word". – Etan Reisner Mar 17 '15 at 19:13
  • Thanks. It's more there as a test case, as I was echoing out the $args variable. But thanks for the heads up. – jasonmclose Mar 17 '15 at 19:21

1 Answers1

0

Apparently, the quotes were confusing bash. The following works...

ip=$1
shift
$(ssh -o ConnectTimeout=1 User@$ip "$@")
jasonmclose
  • 1,667
  • 4
  • 22
  • 38