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' |
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
|
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. |
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. |
Quote:
|
All times are GMT -5. The time now is 12:41 AM. |