![如何比较当前密码与新密码 mac - dscl](https://linux22.com/image/1615802/%E5%A6%82%E4%BD%95%E6%AF%94%E8%BE%83%E5%BD%93%E5%89%8D%E5%AF%86%E7%A0%81%E4%B8%8E%E6%96%B0%E5%AF%86%E7%A0%81%20mac%20-%20dscl.png)
我正在编写一个脚本来更改我们所有 Mac 上的管理员密码。
它是一个基本脚本,但我想在尝试更改密码之前检查密码是否正确:
if [ currentPwd == newPwd ] then
echo 'password is correct'
else
#change account password
fi
这是我用来更改密码的行:
#Change management account locally on mac
sudo dscl . passwd /Users/"${accountName}" "${currentPassword}" "${newPassword}"
echo " Local Admin Account - Password changed! "
这没问题,但我想先检查一下密码是否已被更改。我该怎么做?
答案1
使用这个,加上引号:注意单个=
if [ "$currentPassword" = "$newPassword" ]; then
echo already changed
else
sudo dscl ...
echo Password changed
fi
答案2
这解决了我的问题:
#Check for authentication
sudo dscl /Local/Default -authonly "${accountName}" "${currentPassword}" 2>&1 /dev/null
if [ "$?" != "0" ]; then
#Exit code was not 0 - password has already been changed so no further action
echo "No action taken, account already updated! "