BASH: Check in /etc/shadow if user password is locked

bashlinuxregular expressionusers

Objective: Check in /etc/shadow if user password is locked, i.e. if the first character in the 2nd field in /etc/shadow, which contains the user's hashed password, is an exclamation mark ('!')

Desired output: a variable named $disabled containing either 'True' or 'False'

Username is in the $uname varable and I do something like this:

disabled=`cat /etc/shadow |grep $uname |awk -F\: '{print$2}'`
# I now have the password and need one more pipe into the check for the character
# which is where I'm stuck. I would like to do like (in PHP syntax):
| VARIABLE=="!"?"True":"False"`

This is a fragment of a script that will be run by Cron with root permissions, so there is access to all desirable information.

Best Answer

Why not just do it all with awk?

awk -F: '/<username>/ {if(substr($2,1,1) == "!"){print "True"} else {print "False"}}' /etc/shadow
Related Question