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

2
  • 1
    Copy/paste your scripts into 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. Commented Jun 10, 2020 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" and "Bash assign output to variable inside here document". Commented Jun 10, 2020 at 18:19

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.