Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

string checking

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 07-02-2008
arghya_owen arghya_owen is offline
Registered User
 
Join Date: May 2008
Last Activity: 9 July 2009, 8:31 AM EDT
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Power string checking

please help me providing the code of checking a string pure numeric or not and pure alphanumeric or not
Sponsored Links
    #2  
Old Unix and Linux 07-02-2008
gaurav123 gaurav123 is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 2 July 2008, 10:05 AM EDT
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
for checking numeric values use:

-eq equal to
-ne not equal to
-lt less than
-le less than or equal to
-gt greater than
-ge greater than or equal to


for string comparison use:

>
<
==
!=
=

for regex use:

[[

]]

let me know if u hav any confusion.
Sponsored Links
    #3  
Old Unix and Linux 07-02-2008
arghya_owen arghya_owen is offline
Registered User
 
Join Date: May 2008
Last Activity: 9 July 2009, 8:31 AM EDT
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
i cant get u....lets think str is a variable...how can i check it is pure numeric or not
    #4  
Old Unix and Linux 07-02-2008
gaurav123 gaurav123 is offline
Registered User
 
Join Date: Jun 2008
Last Activity: 2 July 2008, 10:05 AM EDT
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
try this:

if [[ $str == [0-9][0-9]* ]];then

echo "\$str has numeric value=$str"
else
echo "$str is not numeric"
fi
Sponsored Links
    #5  
Old Unix and Linux 07-02-2008
Tytalus's Unix or Linux Image
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 19 May 2015, 6:39 AM EDT
Location: Scotland
Posts: 594
Thanks: 1
Thanked 28 Times in 27 Posts

Code:
check to see if it passes a basic math test ;-)

i.e. something like expr:

#  expr hello + 1
expr: non-numeric argument

#  expr 2hello + 1
expr: non-numeric argument

#  expr 2 + 1
3

mix this with an if and you're away...

Sponsored Links
    #6  
Old Unix and Linux 07-02-2008
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 23 May 2015, 8:47 PM EDT
Location: /dev/ph
Posts: 4,923
Thanks: 68
Thanked 455 Times in 422 Posts
works with ksh and bash

Code:
if [[ ${str//[0-9]/} ]]; then
   print "ERROR: Not a numeric string"
fi

Sponsored Links
    #7  
Old Unix and Linux 07-02-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 28 January 2015, 8:30 AM EST
Posts: 2,669
Thanks: 0
Thanked 18 Times in 18 Posts
case construct

Code:
a=10
case $a in 
   *[!0-9]*) echo "not number";; 
esac

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
string checking arghya_owen Shell Programming and Scripting 1 07-03-2008 03:27 AM
Ksh Checking if string has 2 characters and does not contain digits? developncode UNIX for Dummies Questions & Answers 1 04-08-2008 03:19 PM
Checking for the presence of a string within another string hidnana Shell Programming and Scripting 1 03-20-2008 06:38 AM
checking for empty string in tcsh pagod Shell Programming and Scripting 3 12-07-2007 04:22 AM
Checking if string contains integer haz Shell Programming and Scripting 7 09-11-2006 02:51 AM



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