gpt4 book ai didi

linux - 在 bash 中使用命令行创建用户

转载 作者:太空宇宙 更新时间:2023-11-04 04:08:10 25 4
gpt4 key购买 nike

我想在自定义命令行安装时创建一个用户帐户,下面的代码几乎做得很好:

#!/bin/bash
#nameofuser stands for the new account
#skel is for the a skel folder outside /etc/skel
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " nameofuser
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null

if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1

else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $nameofuser -c new created user -k $skel
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi

如果输入为空或不符合某些密码规范,我需要第二部分的一些帮助和重新循环。

这是第二部分

我如何确保 if 语句证明了一些东西,例如最小密码长度以及密码是否包含特殊字符和数字或至少包含数字?

如果此检查不符合给定值,如何强制用户重新输入密码?

最佳答案

使用这样的东西:

passwdok=0
while [ $passwdok == 0 ] ; do
read -s -p "Enter password : " password
if [[ ! $password =~ ^.{6,} ]] ; then
echo "Password should be at least 6 characters"
elif [[ ! $password =~ [^A-Za-z0-9] ]] ; then
echo "Password should contain a special character"
else
passwdok=1
fi
done

关于linux - 在 bash 中使用命令行创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20311845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com