Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-11-2010
Registered User
 

Join Date: Mar 2010
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Check if input is an integer or a floating point?

Hiii

I actually intent to check the integer or floating point number input by user i.e. 23, 100, 55.25, 12.50 ..etc. However, when someone input strings or alpha character, my program has to show invalid input.!! Is there any Unix shell script syntax can help me to check ?

Thanking you
Sponsored Links
    #2  
Old 03-11-2010
murugaperumal's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Refer the following URL

http://www.unix.com/shell-programmin...ll-script.html
Sponsored Links
    #3  
Old 03-11-2010
ungalnanban's Avatar
Registered User
 

Join Date: Feb 2010
Location: Chennai
Posts: 142
Thanks: 5
Thanked 17 Times in 6 Posts
Thumbs up

Are you expecting this.
See the following shell script, It will check the given string is integer or float.


Code:
echo "123" | grep "^[0-9]*$"
val=`echo $?`
if [[ $val == 0 ]]
then
        echo "Given string is intiger"
else
        echo "Invalide"
fi
echo "123.3" | grep "^[0-9]*[.][0-9]*$"
val=`echo $?`
if [[ $val == 0 ]]
then
        echo "Given string is float"
else
        echo "Invalide"
fi


Last edited by ungalnanban; 03-11-2010 at 11:52 PM.. Reason: code alignment
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
Floating point Emulator pgmfourms Programming 1 04-22-2009 07:48 AM
Floating Point Exception Mandar123 Linux 3 01-22-2009 08:51 AM
Floating point error in C Hara Programming 2 06-18-2008 05:43 AM
Replace floating-point by integer in awk smadonald1 Shell Programming and Scripting 7 01-31-2008 06:39 AM
floating point addition ravi raj kumar Shell Programming and Scripting 8 12-22-2006 01:47 AM



All times are GMT -4. The time now is 03:23 AM.