Linux Shell: how to check a string whether meets some conditions

Hi, guys.
In Linux Shell script, how can I check a string whether meets some conditions.
e.g.:
If a string str must start with a underscore or a alphabet, and it must contains at least one lowercase, one uppercase, one numeric and one punctuation, and its length must be more than 8 characters long and less than 16 characters long, how should I check it?
I can just use the command grep and my command like this:

a='aBc3o!9203'
echo "$a" | grep  '^[_a-Z]' | grep  '[A-Z]' | grep  '[a-z]' | grep  '[0-9]' | grep  '\W'

It just works when determining whether the correct characters included in the string, and it doesn't include the length check. However, it's so wired and so complex and lengthy.
Does anyone have better ideas?
Thanks!

The english language stating the problem is already longer than your source code, I don't know if it can be made truly short. :smiley: But you're right, grep | grep | grep | grep isn't terribly efficient. You could do it in several case statements and use ${#string} to get its length, or pile it all into an awk statement, which isn't terribly efficient itself, but better than five greps, and easier to see what you're doing.

if echo "$PASS" | awk '/^[_a-zA-Z]/ && /[a-z]/ && /[A-Z]/ && /[0-9]/ && (length($0) > 8) && (length($0) < 16) { X=1 } END { exit(!X) }'
then
        echo "Password is OK"
else
        echo "Password is not OK"
fi

Yes!
You're right!
Thx!