Bash script - Using variables within a heredoc

Please can you help.

I ssh and change to root user on all the servers listed in the file "serverlist" to collect the same information from each server. I then save the output to a csv file. I am running the script below but it doesn't seem to give any output except the commas from the echo statement.  Am I missing something very obvious? I have used this script many times before and it works but I never assinged the commands to variables before. Also, I don't want any newline chars from the command because then it doesn't import well into excel.

#!/bin/bash
>output.log
>excel.csv

for server in `cat serverlist`; do
ssh -q $server <<-EOF 2>&1 >>output.log
sudo su -
info1=$(hostname | awk -F'.' '{print $1}')
info2=$(cat /proc/meminfo | grep MemTotal)
info3=$(cat /proc/cpuinfo | grep "model name" | head -1 | awk -F':' '{print $2}')
info4=$(cat /proc/cpuinfo | grep -c processor)
echo "$info1,$info2,$info3,$info4"
EOF
done

sed -e /^Logged/D -e /^WARNING/D -e /^"Initializing profile"/D -e /^$/D output.log>excel.csv

I also tried to set the variables outside of the heredoc but it sets the vars to the commands from the server it's connecting from and never changes.

This is what I had

#!/bin/bash
>output.log
>excel.csv
info1=$(hostname | awk -F'.' '{print $1}')
info2=$(cat /proc/meminfo | grep MemTotal)
info3=$(cat /proc/cpuinfo | grep "model name" | head -1 | awk -F':' '{print $2}')
info4=$(cat /proc/cpuinfo | grep -c processor)
for server in `cat serverlist`; do
ssh -q $server <<-EOF 2>&1 >>output.log
sudo su -
echo "$info1,$info2,$info3,$info4"
EOF
done

sed -e /^Logged/D -e /^WARNING/D -e /^"Initializing profile"/D -e /^$/D output.log>excel.csv

Thank you
jaysilverheelsAsked:
Who is Participating?
 
woolmilkporcCommented:
Did you escape all '$' within the EOF (the heredoc)? (Escaping the '$' of '{print $[x]}' is optional, but won't do any harm)
backticks are to be escaped,too, should you use them (which I don't recommend):
\` ....... \`
The whole thing is needed to protect the $ from expansion by the local shell, as the remote shell must interpret them.
I tried it and it worked for me.
0
 
Xyptilon2Commented:
Im going to bed now, but is this what you were looking for? (scroll down)

http://www.experts-exchange.com/Programming/System/Linux/Q_21862623.html
0
 
jaysilverheelsAuthor Commented:
Thank you but that is not what I was looking for.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
woolmilkporcCommented:
Hi,
you need to protect the variables in the 'echo' statement, by escaping the '$' with a backslash -
echo "\$info1,\$info2,\$info3,\$info4"
HTH
Cheers
wmp
 
0
 
woolmilkporcCommented:
... I don't know if I needed to mention -
of course you must escape the '$' following 'info[n]=...' also, e.g.

info1=\$(hostname | awk -F'.' '{print $1}')
info2=\$(cat /proc/meminfo | grep MemTotal)  etc.
and it's the first version in your Q (with the variables set inside the here doc) which is the correct one!
 
0
 
jaysilverheelsAuthor Commented:
within the EOF it still doesn't seem to like defining variables even when escaping the $. I've also tried using backticks instead of $(). Is it possible to define vars within EOF?
0
 
jaysilverheelsAuthor Commented:
Thanks very much for the clarification- what you say makes sense so I'm going to give that a shot.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.