Please find below the program to evaluate whether the input is Numeric or Non numeric. This was posted by Mr. Gerard Jones<br>#!/bin/ksh<br>#Program to find the given input is a Number(s) or Any<br>#other character<br>read INPUT<br>export VAR1=`echo $INPUT ¦ awk '{print substr($0,1,1)}'`<br>case $VAR1 in [0-9])<br>echo " Input "$INPUT" is Number "<br>;;<br>*)<br>echo " Input "$INPUT" is not a Number "<br>;;<br>esac<br><br>
This can be done better by using the command regex as per the man pages, but I hav not used this command , I am using sun solaris 2.6 ver, When i tried this cmd , it says cmd not found.<br><br>Can any one let me know how to use regex to solve this problem.<br><br>The above awk program solves the problem to some exetent only, If the input is something like "34sed" it still considers as Numeric.<br>
<FONT FACE=monospace>regex</font> is used by the FMLI menu building system. You can put <FONT FACE=monospace>regex</font> commands into your FMLI menu, but they won't run from the shell.<br><br>However, there is a solution to your problem using the Korn shell. In the Korn shell, you can use <FONT FACE=monospace>${#<i>varname</i>}</font> to get length of a variable. So, the following script strips all letters out of the variables using sed and compares the before and after lengths of the variables.<br><FONT FACE=monospace><br>#!/bin/ksh<br><br>A=abc123<br>B=123ABC<br>C=123<br><br><br>NEWA=`echo $A ¦ sed -e 's/[a-zA-Z]//g'`<br>NEWB=`echo $B ¦ sed -e 's/[a-zA-Z]//g'`<br>NEWC=`echo $C ¦ sed -e 's/[a-zA-Z]//g'`<br><br>if [ ${#NEWA} -ne ${#A} ]<br>then<br> echo "A has letters in it"<br>fi<br><br>if [ ${#NEWB} -ne ${#B} ]<br>then<br> echo "B has letters in it"<br>fi<br><br>if [ ${#NEWC} -ne ${#C} ]<br>then<br> echo "C has letters in it"<br>fi<br></font><br>You could use some variations on the sed command to strip out numbers or letters and then check the lengths of the variables after. For example, the following will strip all numbers out of a variable:<br><FONT FACE=monospace><br>MYNEWVAR=`echo $VAR ¦ sar -e 's/[0-9]//g'`<br><br>if [ ${#VAR} -eq ${#MYNEWVAR} ]<br>then<br> echo "VAR is all letters!"<br>elif [ ${#MYNEWVAR} -eq 0 ]<br>then<br> echo "VAR is all numbers!"<br>elif [ ${#MYNEWVAR} -gt 0 -a ${#MYNEWVAR} -lt ${#VAR} ]<br>then<br> echo "VAR is a mix of numbers and letters!"<br>fi<br></font><br><br>Alternatively, this sort of thing is even easier using perl. If you want a perl example, post back here and let me know <br><br>Hope this helps. <p> <br><a href=mailto: > </a><br><a href= > </a><br>--<br>
0 1 - Just my two bits
Here is Kavi's actual problem: -<br>>May be my question was not put in the right form in<br>>the forum. Pls find below a sample program which i<br>>want to run , which would give you more idea on what i<br>>want.<br>><br>>#!/bin/ksh<br>>#Program to find the given input is a Number(s) or Any<br>>#other character<br>>read INPUT<br>>case $INPUT in<br>>[0-9])<br>>echo " Input is Number "<br>>;;<br>>*)<br>>echo " Input is not a Number "<br>>;;<br>>esac<br>><br>>My problem with this program is it works only for<br>>Numbers between 0-9 not for numbers greater than that<br>>eg 13 or 1100.<br>><br>>It would of great help to me if you can answer this<br>>question.<br>><br>>Thanks for spending time for me.<br>><br>>Best regards<br>>Kavi<br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
A very simple to understand way:<br><FONT FACE=monospace># -------------------------<br>if [ -n $1 ]<br>then<br> case "$1" in<br> 0*¦1*¦2*¦3*¦4*¦5*¦6*¦7*¦8*¦9*)<br> echo "$1 is a number"<br> ;;<br> *)<br> echo "$1 is not a number"<br> ;;<br> esac<br>fi<br></font># -------------------------<br><br>I hope it works...
I would use:<br><br>NON_DIGITS=`echo $INPUT ¦ sed 's/[0-9]//g'`<br>if [ -z "$NON_DIGITS" ]<br>then<br> $INPUT is a number<br>else<br> $INPUT is not a number<br>fi
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.