LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 04-10-2004, 11:40 AM   #1
adelatorre204
LQ Newbie
 
Registered: Dec 2003
Location: Coral Springs, FL
Distribution: suse
Posts: 5

Rep: Reputation: 0
conditional not evaluating correctly, help


Below is the beginning of a script I'm trying to write. The problem is the first conditional [ $net=="209.118.xx" ] evaluates to true.
The actual contents of the "net" variable is the first 3 octets of the ipaddress. There are no lowercase x's in it.

Why is it evaluating to true?

Thanks.

#! /bin/bash
#
ipaddr=`echo $SSH_CLIENT | cut -d' ' -f1`
echo $ipaddr
net=`echo $ipaddr | cut -d'.' -f1,2,3`
echo $net
nameok=`grep $USERNAME /etc/outsideusers`

if [ $net=="209.118.xx" ]
then
echo $net 'net was equal'
exit
elif nameok
then
echo $nameok 'name found'
exit
fi
echo 'logout'
 
Old 04-10-2004, 11:53 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
you are using test - the [ operator - completely wrong. use "if [$net -eq "209.." ] instead. please read the manpage for test for more details. spacing and such like matters a lot, as these are all actually individual programs working together, not just functions in a standard language
 
Old 04-10-2004, 12:06 PM   #3
adelatorre204
LQ Newbie
 
Registered: Dec 2003
Location: Coral Springs, FL
Distribution: suse
Posts: 5

Original Poster
Rep: Reputation: 0
your example "if [$net -eq "209.." ] uses -eq, which all my reading indicates is for integer comparisons. I am doing string comparisons.

Also "man test" does not really show much on this.

Can you please elaborate.
 
Old 04-11-2004, 09:18 AM   #4
adelatorre204
LQ Newbie
 
Registered: Dec 2003
Location: Coral Springs, FL
Distribution: suse
Posts: 5

Original Poster
Rep: Reputation: 0
Well, after about six hours of going through every book I could get my hands on and searching all over the internet, I found the solution. So I'm posting it hear for the benefit of any others.

The key was found in the book "Your Unix: The Ultimate Guide" by Sumitabha Das. on pg 551 under the topic "String Comparison"

"test can be used to compare strings with yet another set of operators. Equality is performed with = and inequality with the C-type operator !=. Like the other "test" operators, *these too should have whitespace on either side.*

So the error/problem in the above posted script was the lack of whitespace on either side of the =.

Adding it caused the script to run as expected.
 
Old 04-11-2004, 10:31 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
spacing and such like matters a lot
 
  


Reply



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
Simple C Program - Evaluating the mean of exam results J_K9 Programming 4 09-29-2005 02:30 AM
CASE conditional la_mafia63 Linux - General 3 07-24-2005 01:02 PM
Paris evaluating feasibility of OSS Tinkster General 8 02-19-2004 07:45 PM
PERL::evaluating names of files and dirs ocularbob Programming 5 08-28-2003 06:26 PM
Evaluating video performance - what is a good FPS rate? J.W. Linux - Hardware 1 06-19-2003 03:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:36 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration