我目前正在尝试创建一个脚本,该脚本在运行这个特定的 bash 脚本时为任何特定的人提供一个菜单。在菜单上,我允许了三个选项(1. 添加用户,2. 删除用户和 3. 退出)。关于添加用户选项,一旦选择,应该有 2 个选项允许用户手动输入新用户的详细信息或通过文本文件添加多个用户。我已经能够创建上述菜单/选项,尽管我无法手动为添加用户和许多用户通过 txt 文件以及删除用户创建任务的脚本/正文。
对我的代码的任何帮助将不胜感激!多谢!
这是我现在的脚本:
#!/bin/bash
clear
echo "########## MENU ############\n"
options=("add_user" "delete_user" "exit")
select opt in "${options[@]}"
do
case $opt in
"add_user")
clear
while [ 1 ]
do
clear
echo "1. Add user manually"
echo "2. Add user via TXT file"
read -p "Enter your choice" ch
case $ch in
1)
read -p "Enter user name : " useradd
read -p "Enter user password:" passwd
echo -e "Successfully added the user"
;;
2)
if [ $(id -u) -eq 0 ]; then
for row in `more $1`
do
username=${row%:*}
password=${row#*:}
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 $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a
user!"
fi
done
else
echo "Only root may add a user to the system"
exit 2
fi
esac
done
;;
"delete_user")
read -p "Enter a User name to delete "UNAME
passwd
deluser $UNAME
echo "User $UNAME has been deleted"
;;
"exit")
break
;;
esac
done