[wplug] Re: BASH question

Bob Schmertz rschmertz at speakeasy.net
Wed Jul 9 21:33:03 EDT 2003


>Ok so let me see if I understand.
>
>************
># Script to bring up Landmark's Internet
># 07-09-2003
># v0.1
>Internetstatus=($(adsl-status |grep up|cut -f 4 -d ' '))
>InternetIP=($(ifconfig ppp0 |grep inet |cut -f 2 -d : |cut -f 1 -d ' '))
>curDate=`date +"%y-%m-%d"`
>curTime=`date +"%H%M"`
>
>if $Internetstatus= up ;then
>   printf The Internet link is up!
>      if [ ! $InternetIP = ($(cat /root/CurrentIP.txt)) ;then
>       echo $InternetIP >/root/CurrentIP.txt
>       echo $InternetIP |mail -s "New IP Address" wisej at pios.com
>      fi
>   ;else
>   echo The Internet link is down:$curDate$-$curTime >>/var/log/messages
>fi
>***********
>1) You were saying that to make a variable equal to a string (such as an IP
>address) I need to run the whole command in backets ie. Subshell
>ex:(ifconfig ppp0 |grep inet |cut -f 2 -d : |cut -f 1 -d ' ') This does work
>now... thx.
>2) My syntax errors; lack of space, lack of closing bracket did not seem to
>fix the next step where I attempt a comparison of $InternetIP to ($(cat
>/root/CurrentIP.txt)) and it fails. Is my logic right but syntax wrong?

Not sure why you left out the closing bracket.  Here's something that seems like it should work:

#!/bin/bash

Internetstatus=$(adsl-status |grep up|cut -f 4 -d ' ')
InternetIP=$(/sbin/ifconfig eth0 |grep inet |cut -f 2 -d : |cut -f 1 -d ' ')
# No outer parens necessary here, though they don't seem to hurt
curDate=`date +"%y-%m-%d"`
curTime=`date +"%H%M"`
CurrentIPFile=/root/CurrentIP.txt

if [ $Internetstatus = up ] ;then # Need brackets for this comparison, also watch spacing
      printf "The Internet link is up!\n" # Need quotes for printf!  Not like echo
      if [ ! $InternetIP = $(cat $CurrentIPFile) ] ;then
         # need closing ].  parentheses outside $(run prog) not necessary
       echo $InternetIP >$CurrentIPFile
       echo $InternetIP #|mail -s "New IP Address" wisej at pios.com
      fi
else
      echo The Internet link is down:$curDate-$curTime
fi

-- 
Cheers,
Bob Schmertz





More information about the wplug mailing list