Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Bash script - Using variables within a heredoc

Posted on 2009-04-23
7
Medium Priority
?
3,012 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:jaysilverheels
  • 3
  • 3
7 Comments
 
LVL 13

Expert Comment

by:Xyptilon2
ID: 24218519
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
 

Author Comment

by:jaysilverheels
ID: 24219676
Thank you but that is not what I was looking for.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24222563
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 24223233
... 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
 

Author Comment

by:jaysilverheels
ID: 24254361
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
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 total points
ID: 24255000
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
 

Author Closing Comment

by:jaysilverheels
ID: 31573861
Thanks very much for the clarification- what you say makes sense so I'm going to give that a shot.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month11 days, 19 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question