如何比较当前密码与新密码 mac - dscl

如何比较当前密码与新密码 mac - dscl

我正在编写一个脚本来更改我们所有 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! "

相关内容