我想创建一个bash脚本,用户可以通过输入两次来选择用户名,密码和确认密码。如果密码不匹配,则应提示用户再次输入密码。如果密码匹配,脚本应该创建一个passwordhash,否则再次询问,直到它正确。
到目前为止,我有以下代码,但我不确定这是否是正确的方法。 以下bash脚本是否有问题?
# read username
read -p "Username: " username
# read password twice
read -s -p "Password: " password
echo
read -s -p "Password (again): " password2
# check if passwords match and if not ask again
while [ "$password" != "$password2" ];
do
echo
echo "Please try again"
read -s -p "Password: " password
echo
read -s -p "Password (again): " password2
done
# create passwordhash
passwordhash=`openssl passwd -1 $password`
# do something with the user and passwordhash
答案 0 :(得分:9)
减少冗长的方法:
#!/bin/bash
read -p "Username: " username
while true; do
read -s -p "Password: " password
echo
read -s -p "Password (again): " password2
echo
[ "$password" = "$password2" ] && break
echo "Please try again"
done