ozum
January 15, 2011, 1:46pm
1
I have prepared a script to submit a string in a txt file.
However there are somethings that I have to check before submitting the string in the txt file.
One of those checks is to determine whether the string entered contains any metacharacters.
I have tried sth like;
echo "string" | grep -v [a-zA-Z0-9]
echo "string" | egrep -v .|,|!
however it doesn't work.
How can i check whether the string entered includes any metacharacters or not?
ozum:
I have prepared a script to submit a string in a txt file.
However there are somethings that I have to check before submitting the string in the txt file.
One of those checks is to determine whether the string entered contains any metacharacters.
I have tried sth like;
echo "string" | grep -v [a-zA-Z0-9]
echo "string" | egrep -v .|,|!
however it doesn't work.
How can i check whether the string entered includes any metacharacters or not?
case "$string" in
*[!a-zA-Z0-9]*) echo "non alphnumeric" ;;
*) echo OK ;;
esac
ozum
January 16, 2011, 4:56pm
3
thank you for the response however
this one didn't work since i 'm using a sentence as a string.
here is the response;
>more alphanumeric.sh
case "$1" in
*[!a-zA-Z0-9]*) echo "non alphnumeric" ;;
*) echo OK ;;
esac
>./alphanumeric.sh "this sentence doesn't contain any metacharacters"
non alphnumeric
>./alphanumeric.sh "this sentence contains * metacharacters"
non alphnumeric
###
also other than the alphanumerics, my sentence can also contain . or , or !
Can you help me with this?
Add whatever characters you want to allow to the pattern, e.g.:
case "$string" in
*[!a-zA-Z0-9\ !]*) echo "not OK" ;;
*) echo OK ;;
esac