The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
check the given string is numeric or not. knowledge_gain High Level Programming 11 02-03-2009 11:25 AM
to check variable if its non numeric sachin.gangadha Shell Programming and Scripting 3 12-06-2007 05:33 PM
Perl code to differentiate numeric and non-numeric input Raynon Shell Programming and Scripting 11 08-04-2007 10:32 AM
How to check for a valid numeric input Vijayakumarpc Shell Programming and Scripting 1 08-04-2007 08:34 AM
How to check a column contain numeric or char data type ?? jambesh Shell Programming and Scripting 12 10-06-2006 10:37 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-20-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Check for numeric inputs

Hi All,

How do i modify the below script such that if the input is numeric, it will give the numeric digit, else it will ouput "0"

Code:
echo "xxx"  | awk '/^[0-9]+$/'
  #2 (permalink)  
Old 08-21-2007
namishtiwari namishtiwari is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2007
Location: Bangalore
Posts: 377
Thumbs up

Quote:
Originally Posted by Raynon View Post
Hi All,

How do i modify the below script such that if the input is numeric, it will give the numeric digit, else it will ouput "0"

Code:
echo "xxx"  | awk '/^[0-9]+$/'
Here is the script that will do for you..

#!/bin/ksh
echo "Digits as input"
read number

test=`echo "$number" | egrep "^[0-9]+$"`

if [ "$test" ]
then
echo "Entered number is a digit"
else
echo "0"
fi

Instead of egrep you can use awk,grep or any command for searching patterns.

Thanks
Namish
  #3 (permalink)  
Old 08-21-2007
bigearsbilly bigearsbilly is offline
Registered User
  
 

Join Date: Feb 2006
Location: Southern England
Posts: 102
typeset -i x

will force $x to be a number
  #4 (permalink)  
Old 08-21-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
hi namishtiwari,

I'm using csh shell. Is there a csh version of your code ?
  #5 (permalink)  
Old 08-21-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
not sure this will work in csh
Code:
..
case $x in
    *[!0-9]*|"") echo "0";;
    *) echo $x;;
esac
  #6 (permalink)  
Old 08-21-2007
Raynon Raynon is offline
Registered User
  
 

Join Date: Sep 2006
Location: Sg
Posts: 350
Hi all,

I think i have some idea of converting your ksh to csh.
See below!

Thanks all for the help!!!

Code:
#!/bin/csh

echo -n "Pls input: "
set number = ($<)

test=`echo "$number" | egrep "^[0-9]+$"`

if [ "$test" ] then
echo "Entered number is a digit"
else
echo "0"
endif
  #7 (permalink)  
Old 08-22-2007
bigearsbilly bigearsbilly is offline
Registered User
  
 

Join Date: Feb 2006
Location: Southern England
Posts: 102
c shell considered harmful
Sponsored Links
Closed Thread

Bookmarks

Tags
grep or

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 03:19 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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