我编写了一个基本脚本,它会询问您是否要创建用户,然后在单独的部分中询问您是否要将新用户添加到wheel组。我认为这可以在一个部分中简化,但这超出了我的能力范围。我尝试过不同的方法,但总是失败。这是基本脚本:
echo "^^^ Would you like to setup a new user? yes or no: "
read NEWUSERADD
if [ "$NEWUSERADD" == "yes" ]; then
echo
echo -n "Enter a username: "
read NAME
useradd -m $NAME
passwd $NAME
else [ "$NEWUSERADD" == "no" ]
echo
read -p "**** Not creating a user. Hit [Enter] to continue ****"
fi
# Add new user to wheel group
echo "^^^ Add new user to the wheel group? yes or no: "
read NEWUSERWHEEL
if [ "$NEWUSERWHEEL" == "yes" ]; then
echo
usermod -aG wheel $NAME
else [ "$NEWUSERWHEEL" == "no" ]
echo
read -p "**** Not adding to wheel group. Hit [Enter} to continue ****"
fi
我试图将第二部分放入第一部分中,询问该用户是否已创建,是否要将其添加到轮组中,如果是,则完成操作。 那可能吗?如果是这样,最好的方法是什么?我觉得这将是另一个 if/else 或 elif 块,但我不知道该怎么做。 任何帮助将不胜感激。
使用嵌套
if
块通过 useradd
选项调用 -g wheel
。
另外,请记住引用所有变量。
read -p "^^^ Would you like to setup a new user? yes or no: " NEWUSERADD
if [ "$NEWUSERADD" = "yes" ]; then
echo
read -p "Enter a username: " NAME
read -p "^^^ Add new user to the wheel group? yes or no: " NEWUSERWHEEL
if [ "$NEWUSERWHEEL" = "yes" ]; then
useradd -m -g wheel "$NAME"
else
useradd -m "$NAME"
fi
passwd "$NAME"
else
echo
read -p "**** Not creating a user. Hit [Enter] to continue ****"
fi