如何删除多个用户?

如何删除多个用户?

如何在我的代码中实现删除多个用户。因此,作为管理员,我可以选择通过 1 条命令删除多个用户:

#!/bin/bash
echo "SELECT $OPTION"   
echo "-------------"   
echo "1- Create username"   
echo "2- Create password"   
echo "3- Delete username"   
echo "4- Exit"   
read -p "enter option 1 2 3 or 4:" option   
case $option in      
1) read -p "Enter username:"        
 adduser $REPLY && echo "Username successfully entered"   ;;      
2) passwd && "Password successfully entered" ;;      
3) read -p "Enter user to be deleted: "         
deluser $REPLY && echo "User deleted"  ;;      
4) exit  ;;         
esac

我得到了这个代码来删除多个用户

read -p "Enter users to be deleted: "  #whitespace-separated list of usernames to be deleted     
for USER in $REPLY; do         
  deluser $USER && echo "User $USER deleted"       
done;;

但是我如何在我的代码中实现这一点,我是否使用 while 循环,有人能告诉我我需要做什么吗?

答案1

readand循环for可以像您展示的那样包含在case语句中。在这里,我将其展示为case3 的替代。

case ...
...
3) read -p "Enter users to be deleted: "  #whitespace-separated list of usernames to be deleted     
    for USER in $REPLY; do         
        deluser $USER && echo "User $USER deleted"       
    done;;

相关内容