how to check string of character

how can i check whether variable contains only character from a-z or A-Z....if my variable contains any alpha numeric, numeric or any character with some special one i.e. *%&@! etcetera etcetera....then it should show me please enter only characters......

Let my variable
var1="abc77}|"

then it should show me "please enter valid character"

Here are a couple of threads which turned up using the search feature. Do use the search feature.

hey vino..thanx a lot.......
but neither of the commands are working perfectly for me........in bash
thing is i should allow only those variable which are string...i.e. contains only character....

Those links were meant to be a starting point. I think you understood the code snippets in each. Try to write one based on your understanding. And the when you face an issue, post the code you have so that we can help you better.

hey....vino.....
if i could able to solve on my own..i sudn't post it here....
so help me out...i tried each post provided in replied to this post by you.....but fortunately ya unfortunately...i didn't find a single method..which will help me out in bash .......if you could help me , then please.....

while implying this one....+([a-zA-Z]) , ?(+|-)+([0-9]) and *([0-9]|[a-zA-Z])*...i am getting
-bash: syntax error in conditional expression: unexpected token `('
-bash: syntax error near `+(['

any idea......????

When you face an issue, you show us what you tried, the error that you got and then we would be in a better shape to help you out.

Anyway, this should get you started.

[/tmp]$ cat try.sh
#! /bin/sh

for sample in abc 123 abc123
do
    if [[ $sample == *[[:alpha:]]* ]] ; then
        echo $sample
    fi;
done
    
[/tmp]$ ./try.sh
abc
abc123
[/tmp]$ 

I leave the rest to you as an exercise. Look under the section 'Pattern Matching' in the man pages of bash for other character classes.

hey.......i did the same thing before posting this let me clear one more time...not successful.......only i sud display abc......
is there any way to find only alphabets.....

for your satisfaction....

[:alpha:] := all letters including digit too

so i request you please go thru the query/questions.....where i clearly mentioned i need only character sets......even though my variable has alphanumeric/numeric/ i sud say please enter valid characters

I always thought the [:alnum:] character class included alphabets and digits.

I am signing off from this thread.

thanx vino,

i tried another method , check this out,

for sample in abc 123 abc123
> do
> sample_test=`echo $sample | awk '/[[:digit:]]|[[: punct:]]/ { print -1}'`
> if [ "$sample_test" = "-1" ]
> then
> echo "$sample is not valid."
> else
> echo "$sample is valid."
> fi
> done

abc is valid.
123 is not valid.
abc123 is not valid.

if not work properly.
please try this. ++3+3 and 1-a4-- are valid.

#! /bin/ksh
for sample in -abc 123 abc123 ++3+3 1-a4--
do
sample_test=`echo $sample | awk '/[:digit:]|[: punct:]/ { print -1}'`
if [ "$sample_test" = "-1" ]
then
echo "$sample is not valid."
else
echo "$sample is valid."
fi
done