string checking | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

string checking

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 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 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 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 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 07-02-2008
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
 
Join Date: Jun 2003
Last Activity: 10 January 2014, 5:37 AM EST
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 07-02-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 28 August 2014, 4:55 AM EDT
Location: /dev/ph
Posts: 4,786
Thanks: 63
Thanked 424 Times in 393 Posts
works with ksh and bash

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

Sponsored Links
    #7  
Old 07-02-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts
case construct

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

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 02:06 PM.