Numeric Validation

Hi,

How to check whether an input to a shell script contain only numeric values.
Is there any way to check against the following characters.
&
(
)
|
\
"
'
<
>
`
I've used the following way.

echo $1 | grep "[^0-9]\{2\}$"
if [ $? -eq 0 ]
then
echo "OK"
else
echo "Error"
fi

But, in this case there are two issues.

1) If the input contains numeric values the input value will be displayed which i don't want.
2) If special characters mentioned above are included, the script fails.

I've given up. I would really appreciate if anyone can help me out.

Thanks,
Sumesh

grep "[^0-9]\{2\}$" > /dev/null
if [ $? -eq 0 ]
then
echo "OK"
else
echo "Error"
fi

1) If the input contains numeric values the input value will be displayed which i don't want.

echo $1 | grep "[0-9]\{2\}$" > /dev/null  

or

if  echo $1 | grep -q "[0-9]\{2\}$"
then
echo "OK"
else
echo "Error"
fi

2) If special characters mentioned above are included, the script fails.

Use quotes for special characters

ur_script '&'
ur_script \'

Aju,Anbu

Thanks for the reply.

I tried your code, but still not through.

I've to do grep against $1 and make sure that it should contain two digits of 0-9 range.

I tried to execute the script like

./shellpgm.sh `

In cases like these, error is thrown.

Pls help me.

Thanks,
Sumesh

I have answered for this in previous reply. Use \`

echo $1 | grep "^[0-9]\{2\}$" > /dev/null
if [ $? -eq 0 ]
then
echo "OK"
else
echo "Error"
fi

use carret (^) outside brackets

Thanks Anbu. When i try executing the script with input preceeded by \ it works. Can u pls tell me how to concatenate \ with $1 and proceed.

Thanks a lot
Sumesh

Do you mean the input itself "\" ?
Then use \\

Aju,Anbu,
Thanks for the replies.
Anbu,
For eg: I executed the script as follows by preceeding with \ for input <

[sabraham@secmstqatmp]# ./shellpgm37.sh \>

Now i get the correct value.

But if i try like

./shellpgm37.sh  \$%#&*

i get error as

./shellpgm37.sh  \$%#&*
[1]     19373
ksh: 0:  not found
[sabraham@secmstqatmp]# Error

Is there any other way to do this.

Thanks,
Sumesh

If you have Python installed:

#!/usr/bin/ksh
input=`echo $1 | /usr/bin/python -c "print raw_input().isdigit()"`
if [ $input == "True" ]; then
   echo "Input is a digit"
fi
....
...
..

./shellpgm37.sh '$%#&*'
use single code while entring special characters as args

Ghostdog74,

Thanks for the reply. I don't have Python.

Aju,Anbu

I tried with single quotes. It worked!! Thanks a lot.

thanks abraham. I really appreciate