How to change user password in Linux using shell scipt. Script has been shown with hard-coded as well as command line username and password. am providing you with the script code.
#!/bin/bash
ROOT_UID=0
E_WRONG_USER=65
E_NO_SUCH_USER=70
SUCCESS=0
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "You need to be logged in as root to perform this operation"
exit $E_WRONG_USER
else
echo "Continue with password change"
fi
username=$1
newpassword=$2
grep -q "$username" /etc/passwd
if [ $? -ne $SUCCESS ]
then
echo "No such user exist"
exit $E_NO_SUCH_USER
fi
echo "$newpassword" | passwd --stdin "$username"
echo "Password for $username changed"
exit 0