LinuxQuestions.org
Help answer threads with 0 replies.
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-17-2012, 06:10 PM   #1
Kashif_Bash
LQ Newbie
 
Registered: Apr 2012
Posts: 20

Rep: Reputation: Disabled
if condition error: integer expression expected


I am trying to run following condition with both variables having numeric values "1,2,3"

Code:
if [ '$NEW_STATE' -gt '$OLD_STATE' ];
when i run it i get following error:

Code:
$NEW_STATE: integer expression expected
Please correct me where I'm doing wrong.

I'm trying to check either New State is greater or Old state.
 
Old 04-17-2012, 06:35 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
'$NEW_STATE' is interpreted literally as the string $NEW_STATE. Use double quotes: "$NEW_STATE". See the debugging tip in my signature.

jlinkels
 
Old 04-18-2012, 02:41 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Also, if using bash I would recommend using (()) instead of [], mainly for readability:
Code:
if (( NEW_STATE > OLD_STATE ))
 
Old 04-18-2012, 04:03 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
-gt is used to compare numbers, > or < are used to compare strings.
 
Old 04-18-2012, 04:06 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
also '$NEW_STATE' will not be evaluated because of ', you should use " instead (or nothing, but I prefer to use ").
So you should write:
Code:
if (( "$NEW_STATE" -gt "$OLD_STATE" ));
if you are really sure, those variables contain integers.
 
Old 04-18-2012, 05:25 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@pan64 - May I suggest you confirm your facts before commenting. Whilst you are correct that when using [] or [[]] that the symbols and switches are as you have stated in post #4,
this is not the case when using (()). Also, when inside this construct you are not required to use the $ for variable expansion.

Please see the below link if you would like to learn more:

http://mywiki.wooledge.org/ArithmeticExpression
 
Old 04-18-2012, 05:37 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by grail View Post
@pan64 - May I suggest you confirm your facts before commenting. Whilst you are correct that when using [] or [[]] that the symbols and switches are as you have stated in post #4,
this is not the case when using (()). Also, when inside this construct you are not required to use the $ for variable expansion.

Please see the below link if you would like to learn more:

http://mywiki.wooledge.org/ArithmeticExpression
thanks for correcting.
 
  


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
bash script error : integer expression expected linuxandtsm Linux - Newbie 7 01-07-2014 06:01 AM
Can not access Global variable in C "expected expression before â??:â?? token" golden_boy615 Programming 1 11-16-2011 02:30 PM
C compiling error: expected expression before ‘{’ token CoderMan Programming 3 03-31-2009 04:31 PM
expected primary expression before... Uint character question. RHLinuxGUY Programming 3 05-12-2006 12:08 PM
Error upgrading kernel, /sbin/mkinitrdi integer expression expected amp2000 Linux - General 2 12-05-2003 05:38 AM

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

All times are GMT -5. The time now is 05:39 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
Open Source Consulting | Domain Registration