LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-19-2010, 04:11 PM   #1
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Rep: Reputation: 16
Question Comparing string with integer


If I have a variable, say xvar, which can take both string and integer value and I want to perform an operation in following 2 conditions:
1. Either xvar is null
2. xvar equals 2

[ -z "$xvar" ] || [ $xvar -eq 2 ] && <some-code>

Doesn't seem to work if xvar takes string values

I know that since I have no restriction on xvar, I can get away with string comparison in second test too, ie
[ -z "$xvar" ] || [ "$xvar" = "2" ] && <some-code>

But, 'Sams teach yourself shell script in 24 hrs' says that [ expr1 -eq expr2 ], if either is string, it assumes 0 value

Is it true ??

Can I get it working with 'integer' comparison ??
 
Old 09-19-2010, 04:37 PM   #2
14moose
Member
 
Registered: May 2010
Posts: 83

Rep: Reputation: Disabled
1. Yes, "-eq $some_alpha_value" will equate to 0.

2. Yes, this makes it impossible to distinguish between "zero" and "not-a-number"

3. Reasonable solution:
Quote:
http://www.justlinux.com/forum/showthread.php?t=107246
Code:
function isNumeric(){ echo "$@" | grep -q -v "[^0-9]" }
Example usage:
Code:
isNumeric "$var" && echo "whatever you want"
 
1 members found this post helpful.
Old 09-19-2010, 07:54 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Or you could use the correct enclosures for arithmetic in bash:
Code:
[[ -z $var ]] || (( var == 2 )) && <do stuff>
 
1 members found this post helpful.
Old 09-20-2010, 12:35 AM   #4
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
Thanks grail for the suggestion, i'll use that
Though, I am pretty confused abt the double enclosure stuff

Can you point me to some good links to understand these
I read that this is new and inbuilt in bash but am not able to figure out the exact difference

@ moose

If I do this, this won't equate to 0 I guess
[ 2 -eq "u" ]
bash: [: u: integer expression expected

Then is it correct to say that "-eq $some_alpha_value" will equate to 0

But this seems to work, [[ 2 -eq u ]], is this what was meant ??
 
Old 09-20-2010, 12:51 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
[ is synonymous with test
[[ is basically an extended version of [ and generally handles a little better with unset variables and the like
(()) is used for arithmetic comparisons and calculations

Code:
((x++))
This will increase the x variable integer value by 1

Have a look here for some info.
 
Old 09-20-2010, 01:23 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by 14moose View Post
3. Reasonable solution:
A simpler bash idiom to determine whether a variable contains an integer is
Code:
[[ $var =~ ^[0-9]+$ ]]
The pattern can be translated as "contains only one or more digits 0-9".

EDIT: not only simpler but faster running -- significant if it is repeated many times.

Last edited by catkin; 09-20-2010 at 01:24 PM.
 
1 members found this post helpful.
  


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
[SOLVED] How to convert string to integer back to string C++ LazerPhreak Programming 1 03-13-2010 07:32 PM
C function not returning an integer after comparing it to a char Reverse Logic Programming 21 01-27-2010 02:12 AM
c convert integer to string? kpachopoulos Programming 5 08-23-2006 11:57 AM
C++ string to integer conversion fatman Programming 6 06-18-2006 08:17 AM
Integer To a String in C Bean101 Programming 2 05-27-2004 04:46 AM

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

All times are GMT -5. The time now is 02:08 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