How to check for special character in a value

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?

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

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

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.