system
December 19, 2006, 7:34am
1
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
aju_kup
December 19, 2006, 7:41am
2
grep "[^0-9]\{2\}$" > /dev/null
if [ $? -eq 0 ]
then
echo "OK"
else
echo "Error"
fi
anbu23
December 19, 2006, 7:43am
3
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 \'
system
December 19, 2006, 8:03am
4
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
anbu23
December 19, 2006, 8:07am
5
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 \`
aju_kup
December 19, 2006, 8:10am
6
echo $1 | grep "^[0-9]\{2\}$" > /dev/null
if [ $? -eq 0 ]
then
echo "OK"
else
echo "Error"
fi
use carret (^) outside brackets
system
December 19, 2006, 8:37am
7
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
anbu23
December 19, 2006, 8:39am
8
Do you mean the input itself "\" ?
Then use \\
system
December 19, 2006, 8:58am
9
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
....
...
..
aju_kup
December 19, 2006, 9:16am
11
./shellpgm37.sh '$%#&*'
use single code while entring special characters as args
system
December 19, 2006, 9:24am
12
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