LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   if statements and case statements not working in bourne shell script (https://www.linuxquestions.org/questions/programming-9/if-statements-and-case-statements-not-working-in-bourne-shell-script-192316/)

mparkhurs 06-11-2004 09:35 AM

if statements and case statements not working in bourne shell script
 
Hello,

I run an ssh server for myself and a few friends, however with a dynamic ip it got annoying... so I found dyndns.org. My domain was locked for "abuse" when the scripot I download from them uipdated too much.

After spending a few hours going through the scripts and programs, I decided to write my own.

It works perfectly, expect for one thing. There is something wrong with the if statement. I changed it to a case statement and it still does not work.

This is the script:

#! /bin/bash

#####################################################
#
# dyndns updater /version 1.0
#
#####################################################

#Set variables
user=user
pass=pass
system=dyndns
host= <host>
wildcard=ON

#Get IP address and filter it
wget -O /root/dyndns/ip.html <URL removed.>
cat /root/dyndns/ip.html | grep Your | cut -b 20-35 > ip.cut

#Make IP address into shell variables.
newip=$(cat /root/dyndns/ip.cut)
oldip=$(cat /root/dyndns/ip.current)

#Compare the IPs to each other
case "$newip" in
'$oldip')
echo "IP is the same, not updating"
exit 0
;;
*)
echo $newip > /root/dyndns/ip.current
#curl -s -u $user:$pass <URL removed, system wouldn't let me post it>;;
esac

Before the case statement was an if statement that looked like this:

if [ $newip -eq $oldip ]
then
echo "IP is the same, not updating"
exit 0
else
echo $newip > /root/dyndns/ip.current
#curl -s -u $user:$pass "members.dyndns.org/nic/update?system=$system&hostname=$host&wildcard=$wildcard"

It always acts as if the newip and oldip variables are differant. but I've checked them, over and over, and it always tries to update (that's why the curl command is commented out, couldn't test it without getting banned otherwise.).

Can anyone tell me what is wrong here? Is there some concept I am totally not understanding, or something I am missing from staring at the shell for so long?

-Michael

jxi 06-11-2004 10:18 AM

single quotes around $oldip means you're comparing the new ip dot quad value with the literal string '$oldip' ... will never be equal :-)

put double quotes around $oldip (prob don't need quotes at all)

jim mcnamara 06-11-2004 10:31 AM

Also treat ip's as strings of numbers - use the = operator not the -eq operator.

mparkhurs 06-12-2004 02:41 AM

Working!

if [ $newip == "$oldip" ]
then
echo "IP is the same, not updating"
exit 0
else
echo $newip > /root/dyndns/ip.current
echo "Ip address updated from $oldip to $newip."
curl -s -u $user:$pass "http://members.dyndns.org/nic/update?system=$system&hostname=$host&wildcard=$wildcard"
fi

It was indeed both the operand (needed yo be ==, as opposed to -eq) and how I was quoting the variables.

Thanks!

-Michael


All times are GMT -5. The time now is 04:44 AM.