Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking a variables contents

Status
Not open for further replies.

Arrumac

Technical User
Jan 30, 2002
8
0
0
GB
Hi,

Is there a way I can check a variable is made up of a mixture of characters and numbers, ie a password.

Regards

Adam
 
Here's one way.
Code:
case $VAR in
    [a-z]*[0-9]*) echo "password ok";;
    [0-9]*[a-z]*) echo "password ok";;
    *) echo "problem with password";;
esac
 
Hi,
I believe your case statement would think

9!a

is a perfectly good password because

the 9 would satisfy [0-9]
the ! would satisfy the *
the a would satisfy the [a-z]

Also

9A

would be flagged as a bad password because Capital 'A' doesn't match [a-z].

typically I use egrep

a=`echo $VAR | egrep "[^0-9A-Za-z]"`
if [ "$a" eq $var ]; then
echo "password can only contain letters and numbers"
else
# we know it now contains letters or digits
b=`echo $VAR | egrep "[0-9]"`
c=`echo $VAR | egrep "[A-Za-z]"`
if [ "$b" eq "$c"] ; then
echo "password is valid"
else
echo "Password must contain both letters and digits"
fi
fi


I am not well verses in SH so if the eq isn't right please use the correct thing. ( maybe == ??)

The egrep is looking for all characters other than digits and letters and if it finds one it will return the string else it will return nothing and nothing means it only contains letters and numbers.

then you need the second set of egreps to make sure it contains both letters and digits. if the user only specifies letters "aaa" or digits "111" one of the greps will return nothing and the 2 strings will not compare.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top