LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-11-2004, 09:35 AM   #1
mparkhurs
LQ Newbie
 
Registered: Jun 2004
Posts: 5

Rep: Reputation: 0
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
 
Old 06-11-2004, 10:18 AM   #2
jxi
Member
 
Registered: Feb 2003
Location: Richmond VA
Distribution: Slackware 11 -- CentOS 4.4
Posts: 115

Rep: Reputation: 15
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)
 
Old 06-11-2004, 10:31 AM   #3
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
Also treat ip's as strings of numbers - use the = operator not the -eq operator.
 
Old 06-12-2004, 02:41 AM   #4
mparkhurs
LQ Newbie
 
Registered: Jun 2004
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if else statements 0.o Programming 7 09-27-2005 02:59 PM
help changing case on arguments to bourne shell script Maldain Programming 2 05-03-2005 10:18 AM
if statements thesnaggle Linux - Software 1 02-16-2004 09:52 AM
How do I use X =< Y w/ IF statements? dolvmin Linux - Software 11 09-24-2003 10:39 PM
if then statements vapor Programming 4 07-16-2003 06:44 AM


All times are GMT -5. The time now is 05:14 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration