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 04-02-2008, 08:22 AM   #1
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Rep: Reputation: 15
abnormal behavious in comparison check in 'if' condition.


Hi

Please look at the following code snipet.

Code:
xxx=0

if [ xxx -eq "0" ]
then
        echo "Im in if"
fi
It goes in the if condition! I fail to understand since xxx does not have a "$" in the condition check.
The shell is ksh and machine is AIX 5.3

Please provide some explanation. I would be very thankful.

Kind regards,

indiancosmonaut
 
Old 04-02-2008, 08:38 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
well, obviously korn assumes $xxx in this case.
though I can't find any documentation.

if you use /usr/bin/test instead it fails.
 
Old 04-02-2008, 09:16 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I would imagine that when performing integer comparison (-eq and friends), any non-integer operands take the value of 0. Don't you get some warning message about it?
 
Old 04-02-2008, 09:36 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
not so.
it must be a ksh feature!

Code:
$xxx=12
$ if [ xxx -eq 12  ]; then echo in if;fi 
in if

Code:
$ bash
bash-3.2$ xxx=12
bash-3.2$ if [ xxx -eq 12  ]; then echo in if;fi
bash: [: xxx: integer expression expected
bash-3.2$ if [ $xxx -eq 12  ]; then echo in if;fi
in if
/bin/sh
Code:
bash-3.2$ /bin/sh
$ xxx=12
$ if [ xxx -eq 12  ]; then echo in if;fi
$  if [ $xxx -eq 12  ]; then echo in if;fi
in if
 
Old 04-02-2008, 09:48 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
that's far out
 
Old 04-02-2008, 12:05 PM   #6
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks gentlemen, for your analysis. I think you found a new thing in ksh. :-)

Kind regards,

indiancosmonaut
 
Old 04-02-2008, 12:24 PM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
yes!
3 different results for 3 different, sort-of, compatible shells.

(on solaris anyway, /bin/sh is actually bash on linux)
 
Old 04-02-2008, 07:47 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Hey BB, what do those 3 get if you use [[ ]] i.e. double brackets.?
I don't have access to all 3 shells, but was taught way back in a ksh course that [[ ]] is better.
 
Old 04-03-2008, 12:25 AM   #9
indiancosmonaut
Member
 
Registered: Feb 2007
Posts: 65

Original Poster
Rep: Reputation: 15
Hi,

In ksh it works with double square brackets also.

Code:
$ xxx=12
$ if [ xxx -eq 12 ]; then echo true!; fi
true!
$ if [[ xxx -eq 12 ]]; then echo true!; fi
true!
$
Is there a reason for preferring [[ ]] instead of []?

Kind regards,

indiancosmonaut
 
Old 04-03-2008, 12:34 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
I think [[ is more of a macros than a command-line program that accepts parameters like [ or test.
I personally prefer [[ in bash than [ when it comes to arithmetic comparisons but use [ when it comes to string tests.
 
Old 04-03-2008, 01:10 AM   #11
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
double square brackets test is more robust: http://www.tldp.org/LDP/abs/html/tes...ml#DBLBRACKETS
 
  


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
Makefile:condition to check whether a file exists ? Ashok_mittal Linux - Newbie 4 12-06-2011 08:52 PM
Can I use 'if' statements in Expect script? d3funct Programming 3 02-24-2011 04:59 PM
String comparison in while [ ... ] condition hiwa Linux - Software 3 05-27-2007 03:05 AM
Abnormal traffic? xathras Linux - Security 4 05-08-2005 05:13 PM
Abnormal font sizes... Nazxul Linux - Software 3 02-06-2005 02:28 PM


All times are GMT -5. The time now is 08:45 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration