0

I know there are too many question regarding saving awk output to variable available on stack overflow, but I've tried all the possible answers but non seems to be working. Please help me with the following piece of code.

I've attemped the following types of solutions, but all of them give me blank output when echo.

Case 1:

sshpass -p$password ssh -T -o StrictHostKeyChecking=no $hostname <<EOF
          tomcat=`ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print \$2}' `
          httpd=`systemctl status httpd | awk 'NR==3 {print \$2}'`
          echo $tomcat
          echo $httpd
EOF

Case 2:

sshpass -p$password ssh -T -o StrictHostKeyChecking=no $hostname <<EOF
          tomcat=$(ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print \$2}')
          httpd=$(systemctl status httpd | awk 'NR==3 {print \$2}')
          echo $tomcat
          echo $httpd
EOF

Case 3:

sshpass -p$password ssh -T -o StrictHostKeyChecking=no $hostname <<EOF
          tomcat=`ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print $2}' `
          httpd=`systemctl status httpd | awk 'NR==3 {print $2}'`
          echo $tomcat
          echo $httpd
EOF

Case 4:

sshpass -p$password ssh -T -o StrictHostKeyChecking=no $hostname <<EOF
          tomcat=$(ps -ef | grep -i tomcat | grep -i bootstrap | awk '{print $2}')
          httpd=$(systemctl status httpd | awk 'NR==3 {print $2}')
          echo $tomcat
          echo $httpd
EOF

Please help me out.

Thanks,

Sid

Cyrus
  • 84,225
  • 14
  • 89
  • 153
  • 1
    Copy/paste your scripts into http://shellcheck.net, fix the issues it tells you about, and then [edit] your question to show the fixed scripts if you still have a problem. – Ed Morton Jun 10 '20 at 17:23
  • 1
    The problem is that the variable and command substitutions are being processed on the local computer, before the commands are send over `ssh` (and hence before the variables are even assigned, and on a different computer from where they eventually will be assigned). This Q is a mostly-duplicate of ["Quotes within HERE document in shell scripts"](https://stackoverflow.com/questions/8076183/quotes-within-here-document-in-shell-scripts) and ["Bash assign output to variable inside here document"](https://stackoverflow.com/questions/36250237/bash-assign-output-to-variable-inside-here-document). – Gordon Davisson Jun 10 '20 at 18:19

0 Answers0