0

I am writing a shell script which contains below line -

sshpass -p "pwd" ssh -t hostname@$ip1 ' cd /path/to/home/oflops/cbench; ./cbench -c $ip2 -p 6633 -m 1000 -l 10 -M 1000 -s 1 -t; ' >> file

The above line works fine in shell but does not work in script. Please help, I am new to script writing and not able to find out my mistake.

  • I am not getting any error. The problem is the commands which has to be run in shell after ssh are not working. – user3592429 Feb 18 '15 at 07:05
  • How do you know it is not working? – anubhava Feb 18 '15 at 07:06
  • These commands "cd /path/to/home/oflops/cbench; ./cbench -c $ip2 -p 6633 -m 1000 -l 10 -M 1000 -s 1 -t;" are not running. – user3592429 Feb 18 '15 at 07:07
  • These commands are not running because I am not able to see the output generated by the commands in the output file. – user3592429 Feb 18 '15 at 07:08
  • "commands are not running" is not always equivalent to "not able to see the output".... Just sayin'. But more to your question, `$ip2` is inside single quotes and will thus not be expanded. – twalberg Feb 18 '15 at 21:23

2 Answers2

0

Run your command like this:

sshpass -p "pwd" ssh -t hostname@$ip1 "cd /path/to/home/oflops/cbench; ./cbench -c $ip2 -p 6633 -m 1000 -l 10 -M 1000 -s 1 -t;" >> file

i.e. using double quotes for your command rather than single quote to be able to expand variables like $ip2

anubhava
  • 761,203
  • 64
  • 569
  • 643
-1

"sshpass" command was not running in bash script. I changed the above script as follows to make it run -

`sshpass -p "pwd" ssh -o StrictHostKeyChecking=no hostname@$ip1 "cd /path/to/home/oflops/cbench; ./cbench -c $ip2 -p 6633 -m 1000 -l 10 -M 1000 -s 1 -t;" >> file`
  • Adding backticks around a command does not "make it run" in any way that it wouldn't have run otherwise. Rather, what it does is take the command's output, string-split and glob-expand that output, and run the result as a second command itself. Also, since you're redirecting output to a file, it's guaranteed that there's no output for the backticks to interpret, meaning that they can't have any possible effect. – Charles Duffy Feb 18 '15 at 17:06
  • ...that said, I wouldn't be surprised if suppressing the connection to stdin might have done the trick, but if that's what actually fixed your problem, you'd want to do it directly (with ` – Charles Duffy Feb 18 '15 at 17:07