The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Converting integer to String ROOZ Shell Programming and Scripting 1 06-05-2008 02:38 PM
Checking if string contains integer haz Shell Programming and Scripting 7 09-11-2006 03:51 AM
conersting string to integer abb058 UNIX for Dummies Questions & Answers 2 08-23-2006 08:52 AM
Integer to String psilva High Level Programming 2 08-17-2001 01:14 PM
convert from an integer to a string mojomonkeyhelper UNIX for Dummies Questions & Answers 6 03-29-2001 05:15 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-28-2008
kaushys kaushys is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 22
Comparing string and integer in IF

hi,
I need to create an IF condition. I read a line from a file and get the 5 word using space as a delimited. This word can have only two values either '*' or '1-5'

I need to write an IF condition for two cases. I can either compare it to * or 1-5(or even 1 by cutting and getting only the first character).
But when I write it

eg, if [$var -eq '*' ]

If the value is 1-5 it says integer argument expected. If I write

if [ $var == 1 ]

In this case when the value is '*' it says string expected. How can I resolve this. Cannt I declare that the $var is always goiing to be a char. So I can use -eq '*' and -eq '1-5'.
  #2 (permalink)  
Old 07-28-2008
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,427
The -eq operator is for numeric test.
For a string test, you must use the = operator.
Code:
if [ "$var" = '*' ]
if [ "$var" = '1-5' ]
Jean-Pierre.
  #3 (permalink)  
Old 07-28-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink Beware of how you compare...

Code:
> var1="*"
> if [ "$var1" = "*" ] ; then echo "match"; fi
match
> if [ "$var1" = "1" ] ; then echo "match"; fi
> 

> var1="1"
> if [ "$var1" = "*" ] ; then echo "match"; fi
> if [ "$var1" = "1" ] ; then echo "match"; fi
match

> var1=1
> if [ "$var1" = "1" ] ; then echo "match"; fi
match
  #4 (permalink)  
Old 07-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Or you could simply use case instead.

Code:
case $var in '*') echo "It's full of stars";; 1-5) echo "Working one to five";; esac
Closed Thread

Bookmarks

Tags
conditionals, string vs integer

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:34 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0