Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test if numeric 1

Status
Not open for further replies.

wube

MIS
Sep 6, 2001
4
US
I am currently writing a script that expects input to be keyed in, and I have to verify if the input is numeric or not.
#!/bin/sh
read input
echo $input | awk ' { if ( $0 ~ /[0-9]/ )
printf " input contains numerics only"
else printf " input is mixed mode " } '

The results of testing are as follows :

input result
123 numerics only
123a numerics only
a22 numerics only

How does one check if the whole input is numeric ??
 
Change your regular expression to:

/^[0-9]*$/

Hope this helps.

CaKiwi
 
Thanks. That worked. I was on the right track. I did try the following combinations:
/^[0-9]/
/[0-9]+$/
but these did not work.
Once again, thanks a lot.

wube
 
Actually, my solution will treat empty lines as numeric. This will treat them as mixed.

/^[0-9]+$/

CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top