Que tal:
if [ 'passwd -S ${i} | cut -d" " -f2' == "P" ]; then do some stuff; fi
Estou escrevendo um script e preciso verificar $ user se a conta dele tiver uma senha definida, como posso fazer isso?
Eu sei disso:
passwd --status username
Display account status information. The status information consists of 7 fields. The first field is the user's login name. The second field indicates if the user account has a locked password (L), has no password (NP), or has a usable password (P). The third field gives the date of the last password change. The next four fields are the minimum age, maximum age, warning period, and inactivity period for the password. These ages are expressed in days.
mas preciso de algo que possa caber em if test
Aqui está o código:
userPass()
{
for i in "#@"
do
if [ "$i" = root ]
then
continue
else
echo "Changing password for $i:"
echo $i:$i"YOURSTRONGPASSWORDHERE" | chpasswd
if [ "$?" = 0 ]
then
echo "Password for user $i changed successfully"
fi
fi
done
}
userPass $1 $2 $3
Como a saída de passwd --status
indica se o usuário não tem senha ("não tem senha (NP)"), você pode verificar isso:
if [[ $(passwd --status "$i" | awk '{print $2}') = NP ]]
then
echo "$i doesn't have a password."
fi
Ou:
case $(passwd --status "$i" | awk '{print $2}') in
NP) echo "$i doesn't have a password."
# set password here.
;;
L) echo "$i's account is locked." ;;
P) echo "$i has a password." ;;
esac
Também podemos fazer check-in em /etc/password
se tivermos acesso.
if [ 'awk -F ':' '/^'$i':/ {print $2}' /etc/shadow' ] ; then
echo "User has passwd" ;
else
echo "NO passwd";
fi
Tags password bash shell test shell-script