Home >Operation and Maintenance >Linux Operation and Maintenance >How to assign password to user in shell script

How to assign password to user in shell script

不言
不言Original
2019-03-22 14:41:253342browse

This article introduces to you how to assign passwords to users in shell scripts. Let’s look at the specific content below.

How to assign password to user in shell script

Let’s take a look at the command first

echo <password> | passwd –stdin <username>

For example:

Use the following command to change the password of user jack in the shell script . For example, we use the string password as the password.

cho "password" | passwd --stdin user

Assign user to enter password:

Use the following command to enter password from user and assign to user user.

read -p "Enter Password for User jack: " pwd
echo $pwd | passwd --stdin user

In addition, we can also prompt the user to enter the password twice to confirm that the user has entered the correct password. Use the following command to enter the password twic from user and assign it to user user.

while :
do
    read -p "Enter Password for User user: " pwd1
    read -p "Confirm Password for User user: " pwd2
    if [ "$pwd1" == "$pwd2" ]
    then
		break
    else
	    echo "Password and Confirm password doesn&#39;t match...."
    fi
done
echo $pwd1 | passwd --stdin user

This article has ended here. For more other exciting content, you can pay attention to the Linux Tutorial Video column of the PHP Chinese website!

The above is the detailed content of How to assign password to user in shell script. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn