Hi,
I have a variable and to it always alphanumeric value will be assigned.
If the value has any special characters in it then in the if statement it should exit like below
if (value has any speacial character)
then
exit
else
....
fi
can any one suggest how to acheive this?
Tytalus
November 12, 2009, 5:30am
2
something like:
# var=1234a ; [ ! $(echo $var|grep "[^0-9]") ] && echo $var
# var=12345 ; [ ! $(echo $var|grep "[^0-9]") ] && echo $var
12345
chould get you on the right path
panyam
November 12, 2009, 5:39am
3
Did not work for me , might be the issue with the shell :o
how ever with out negate worked fine.
TES>var=1234; [ ! $(echo $var | grep "[^0-9]") ] && echo $var
sh: test: Specify a parameter with this command.
TES>var=1234a; [ $(echo $var | grep "[^0-9]") ] && echo $var
1234a
danmero
November 12, 2009, 6:08am
4
For bash / ksh
var='1234a;'
if [ ! -z ${var//[[:alnum:]]/} ]
then
exit
else
echo OK; # Do something here
fi
hi,
thanks. It worked for me..
I have to add one condition while checking for the special characters. That is if there is any comma then it should go to else part. if there is any other special characters other than comma then the script has to exit.