admin管理员组

文章数量:1277317

I am having trouble execting the below command on remote SSH. Trying to execute 3 commands on remote server and returning its output as CSV value. If the run the command on the command line it works well. But when i save it and run it as shell script, the variable values are not getting printed. However it prints the =s...

s=$(ssh -q user@host "a=$(free -g | grep Mem | awk '{print $2}' | sed 's/ //g') && b=$(lscpu | grep "^CPU(s)" | awk '{print $2}' | sed 's/ //g') && c=$(hostname) && echo ===$a===$b===$c===")

echo $s

Please do suggest if there is a better way to achieve this.

Another peculear thing I noted is in c, where i am trying to print hostname, it prints the kernel version instead of host name.

I am having trouble execting the below command on remote SSH. Trying to execute 3 commands on remote server and returning its output as CSV value. If the run the command on the command line it works well. But when i save it and run it as shell script, the variable values are not getting printed. However it prints the =s...

s=$(ssh -q user@host "a=$(free -g | grep Mem | awk '{print $2}' | sed 's/ //g') && b=$(lscpu | grep "^CPU(s)" | awk '{print $2}' | sed 's/ //g') && c=$(hostname) && echo ===$a===$b===$c===")

echo $s

Please do suggest if there is a better way to achieve this.

Another peculear thing I noted is in c, where i am trying to print hostname, it prints the kernel version instead of host name.

Share Improve this question asked Feb 25 at 12:01 Sudharsan SimhanSudharsan Simhan 271 silver badge9 bronze badges 1
  • Please take a look at the description of the "shell" tag you applied. In short, the first thing is to find out which shell you're using to get into the right scope. Then, just a trick for your toolbox: If a program call gives you issues, add an echo in front to see how exactly the shell substituted your placeholders. Also, more generally, it helps if you reduce the scope of your problem. The above code connects ssh, free, grep, awk, sed, lscpu, and hostname, which exactly produces unexpected results? – Ulrich Eckhardt Commented Feb 25 at 12:51
Add a comment  | 

1 Answer 1

Reset to default 2
#!/bin/bash


s=$(ssh ttdev 'a=$(free -g | grep Mem | awk "{print \$2}" | sed "s/ //g") && b=$(lscpu | grep "^CPU(s)" | awk "{print \$2}" | sed "s/ //g") && c=$(hostname) && echo ===$a===$b===$c===')

echo $s

ttdev is a name of ssh configuration to my server done under ~/.ssh/config.
The script should work with user@host as per your case as well.

本文标签: shellRemote SSHNot printing the variablesStack Overflow