Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a script that would ssh into the VMWare server and check if the VMs are up or not. There's some error in the server=awk "/$i/{print $2}" /home/dharmit/scripts/vmids line. I am getting following error

./server_notify.sh: line 38: /688/{print }: No such file or directory

I know that I have made some error in quoting. But I can't find what the error is.

for i in {144,528,544,576,592,624,640,688}
do
        ssh root@some.ip vim-cmd vmsvc/power.getstate $i | grep off > /dev/null
        if [ $? -eq 0 ]
        then
                server=awk "/$i/{print $2}" /home/dharmit/scripts/vmids
                echo "$server is Down"
        fi
done

Contents of /home/dharmit/scripts/vmids

144 Web Server
528 N-Zimbra Mail Server
544 Zenoss
576 OTRS Supporting Tool
592 Proconsol
624 SVN
640 SFTP
688 FTP

Can someone please point me where I am making a mistake and what the solution is?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You're missing backquotes around the awk command and the double quotes are allowing $2 to be interpolated by the shell instead of awk. The server= line should read:

server=`awk "/$i/"' {print $2}' /home/dharmit/scripts/vmids`

The double quotes allow $i to be interpolated by the shell, which is what you apparently want. The single quotes prevent shell interpolation of $2. The backquotes cause the awk command to be run and its result turned into a string that becomes the value of the server variable.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.