0

I'm trying to execute a local file (here /tmp/cmd) on a remote machine (slc05bkt) using SSH. Here's the command I'm trying from a script file:

cmd="ssh -v ssuthari@slc05bkt \"bash -l -s\" < /tmp/cmd"
$cmd

It's giving me the following exception:

ksh: line 1: /tmp/cmd: cannot open [No such file or directory]

Even after I added a copy of this local file to remote machine in the same location, it's giving me one more error :

ksh: line 1: bash -l -s: not found

The irony here is I'm able to execute the command directly on command line, but I'm unable to execute above lines from the script. Can someone please help me out on solving this issue?

  • what is the content of the file you copy to remote location ? – Archemar Sep 4 '15 at 13:13
  • 1
    Don't put commands in variables. – Cyrus Sep 4 '15 at 17:03
  • @Archemar- The file I'm passing to SSH i.e. /tmp/cmd here contains the command i want to execute on remote machine. – Sandeep Suthari Sep 7 '15 at 4:50
1

$cmd does not evaluate your variable, it just expands it and passes to the shell as a string, this is why you are getting errors. In your case, try eval "$cmd".

And in any case, I'd suggest to use $(ssh the_rest_of_your_command_line) instead of eval "$cmd", since eval is a bad habit.

  • Even with $(ssh rest_of_command_line) is giving me the above errors.With eval,its working.Can you tell me why using eval is a bad habit? – Sandeep Suthari Sep 7 '15 at 5:06
  • Well, to investigate why $(ssh …) is failing I need to see the context. eval() is bad since it executes anything you feed it, so it's a possibility to run something you didn't exepect. – galaxy Sep 7 '15 at 12:19
  • @galaxy-- I'm trying the following code : execCmd() { HOST=$1 IS_SCRIPT=$2 cmd="-v $USER@$HOST \"bash -l -s\" < $3" $(ssh $cmd) } execCmd "slc05bkt" "true" /tmp/cmd2 The file '/tmp/cmd2' is a local file which has the command to be executed on the remote machine slc05bkt. – Sandeep Suthari Sep 8 '15 at 6:45
  • As per my answer, the $cmd variable is going to be just expanded, so the redirection won't be performed. It will work if you remove the redirection from the variable and add it at the end of the $(). Additionally to this $() is expected to be assigned to a variable to catch output. Your whole script you presented here could be redefined to execCmd(){cat "$3" | ssh -v $USER${USER:+@}${1:-localhost} } ; execCmd "slc05bkt" "true" /tmp/cmd2 – galaxy Sep 8 '15 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.