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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Evaluate input in a shelll program 3

Status
Not open for further replies.

gkd

MIS
Jun 20, 2000
25
SG
How to find out if the input of a variable is a number or any other character?
 
echo $var ¦ awk '$1 * $1 &gt; -1 {print $0}'<br><br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
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 &quot; Input &quot;$INPUT&quot; is Number &quot;<br>;;<br>*)<br>echo &quot; Input &quot;$INPUT&quot; is not a Number &quot;<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 &quot;34sed&quot; it still considers as Numeric.<br>
 
<FONT FACE=monospace>regex</font> is used by the FMLI menu building system.&nbsp;&nbsp;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.&nbsp;&nbsp;In the Korn shell, you can use <FONT FACE=monospace>${#<i>varname</i>}</font> to get length of a variable.&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;A has letters in it&quot;<br>fi<br><br>if [ ${#NEWB} -ne ${#B} ]<br>then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;B has letters in it&quot;<br>fi<br><br>if [ ${#NEWC} -ne ${#C} ]<br>then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;C has letters in it&quot;<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.&nbsp;&nbsp;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>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;VAR is all letters!&quot;<br>elif [ ${#MYNEWVAR} -eq 0 ]<br>then<br>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;VAR is all numbers!&quot;<br>elif [ ${#MYNEWVAR} -gt 0 -a ${#MYNEWVAR} -lt ${#VAR} ]<br>then<br>&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;VAR is a mix of numbers and letters!&quot;<br>fi<br></font><br><br>Alternatively, this sort of thing is even easier using perl.&nbsp;&nbsp;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>&gt;May be my question was not put in the right form in<br>&gt;the forum. Pls find below a sample program which i<br>&gt;want to run , which would give you more idea on what i<br>&gt;want.<br>&gt;<br>&gt;#!/bin/ksh<br>&gt;#Program to find the given input is a Number(s) or Any<br>&gt;#other character<br>&gt;read INPUT<br>&gt;case $INPUT in<br>&gt;[0-9])<br>&gt;echo &quot; Input is Number &quot;<br>&gt;;;<br>&gt;*)<br>&gt;echo &quot; Input is not a Number &quot;<br>&gt;;;<br>&gt;esac<br>&gt;<br>&gt;My problem with this program is it works only for<br>&gt;Numbers between 0-9 not for numbers greater than that<br>&gt;eg 13 or 1100.<br>&gt;<br>&gt;It would of great help to me if you can answer this<br>&gt;question.<br>&gt;<br>&gt;Thanks for spending time for me.<br>&gt;<br>&gt;Best regards<br>&gt;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>&nbsp;&nbsp;case &quot;$1&quot; in<br>&nbsp;&nbsp;&nbsp;&nbsp;0*¦1*¦2*¦3*¦4*¦5*¦6*¦7*¦8*¦9*)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;$1 is a number&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;;;<br>&nbsp;&nbsp;&nbsp;&nbsp;*)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;$1 is not a number&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;;;<br>&nbsp;&nbsp;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 &quot;$NON_DIGITS&quot; ]<br>then<br>&nbsp;&nbsp;&nbsp;&nbsp;$INPUT is a number<br>else<br>&nbsp;&nbsp;&nbsp;&nbsp;$INPUT is not a number<br>fi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top