shell编程教学(四) | shell编程习题实操(二)提高篇

前言

有了shell编程教学(四) | shell编程习题实操 (一)的练习基础,可以进行更高难度的shell编程了。以加深对shell编程的理解!

习题实操

要求:
写一个名为User_Manager.sh的脚本,是能够建立用户,删除用户!(尽量让用户使用更方便)

我写的代码,仅供参考!不懂的可以留言答疑哦!
法一:

  1 #############################
  2 # Author:    Allen          #
  3 # Create_Date:2019-03-08    #
  4 # Version:                  #
  5 # Mail:                     #
  6 # Description:              #
  7 #############################
  8 
  9 #!/bin/bash
 10 
 11 Input_Action()
 12 {
 13         read -p "[C]reate [D]elete [E]xit  :" ACTION
 14         ACTION=`echo $ACTION | tr 'a-z' 'A-Z'`
 15         echo $ACTION
 16         [ "$ACTION" != "C" -a "$ACTION" != "D" -a "$ACTION" != "E" ] && {
 17         Input_Action
 18         }
 19         [ "$ACTION" = "C" ] &&{
 20         Check_Username
 21         }
 22         [ "$ACTION" = "D" ] && {
 23         Delete_Username
 24         }
 25         [ "$ACTION" = "E" ] && {
 26         echo "退出!"
 27         exit
 28         }
 29 }
 30 
 31 Check_Username()
 32 {
 33         read -p "请输入你要创建的用户名: " USERNAME
 34         id $USERNAME &> /dev/null && {
 35         echo $USERNAME 已经存在!
 36         Input_Action
 37         }||{
 38         Create_Username
 39         }
 40 }
 41 
 42 Create_Username()
 43 {
 44         useradd $USERNAME
 45         read -p "请输入密码: " -s PASSWORD
 46         echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && echo 创建成功!
 47         echo "#########################################"        
 48         Input_Action
 49 }
 50 
 51 Delete_Username()
 52 {
 53         echo "你有以下用户:"
 54         grep "bash" /etc/passwd | awk -F : '{print $1}'
 55         read -p "你想删除哪个用户? " USERNAME
 56         id $USERNAME &> /dev/null && {
 57         userdel -r $USERNAME && {
 58                 echo "$USERNAME 删除成功!"
 59                 echo "#########################################"
 60                 Input_Action
 61                 }
 62         }||{
 63         echo "没有 $USERNAME 用户"
 64         Input_Action
 65         }
 66 }
 67 
 68 echo 请输入一个动作!
 69 read -p "[C]reate [D]elete [E]xit  :" ACTION
 70 ACTION=`echo $ACTION | tr 'a-z' 'A-Z'`
 71 echo "$ACTION"
 72 echo "#########################################"
 73 
 74 [ "$ACTION" != "C" -a "$ACTION" != "D" -a "$ACTION" != "E" ] && {
 75         echo 输入错误!请重试!
 76         Input_Action
 77 }
 78 
 79 [ "$ACTION" = "C" ] && {
 80         read -p "请输入你要创建的用户名: " USERNAME
 81         id $USERNAME &> /dev/null && {
 82         echo $USERNAME 已经存在!
 83         Check_Username
 84         }||{
 85         read -p "请输入密码: " -s PASSWORD
 86         useradd $USERNAME
 87         echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && echo 创建成功!
 88         echo "#########################################"
 89         Input_Action
 90         }
 91 
 92 }
 93 
 94 [ "$ACTION" = "D" ] && {
 95         echo "你有以下用户:"
 96         grep "bash" /etc/passwd | awk -F : '{print $1}'
 97         read -p "你想删除哪个用户? " USERNAME
 98         id $USERNAME &> /dev/null && {
 99         userdel -r $USERNAME && echo "$USERNAME 删除成功!"
100         echo "#########################################"
101         Input_Action
102         }||{
103         echo "没有 $USERNAME 用户"
104         Delete_Username
105         }
106 }
107 
108 [ "$ACTION" = "E" ] && {
109         echo "退出!"
110         exit
111 }

效果:
在这里插入图片描述
法二:

  1 #!/bin/bash
  2 while true
  3 do
  4         echo "请输入一个动作:"
  5         read -p "[C]reate [D]elete [E]xit :" ACTION
  6         ACTION=`echo $ACTION | tr "a-z" "A-Z" `
  7 
  8         case $ACTION in
  9                 C)
 10                 read -p "请输入用户名:" USERNAME
 11                 id $USERNAME &> /dev/null && {
 12                         echo -e "\033[31m $USERNAME 已经存在!\033[0m "
 13                         }||{
 14                         read -p "请输入密码: " -s PASSWORD
 15                         useradd $USERNAME
 16                         echo $PASSWORD | passwd --stdin $USERNAME &> /dev/null && ec    ho " $USERNAME 创建成功!"
 17                         echo "#########################################"
 18                         }
 19                 ;;
 20                 D)
 21                 echo `grep "bash$" /etc/passwd | awk -F : '{print $1}'`
 22                 read -p "请输入你要删除的用户名:" USERNAME
 23                 id $USERNAME &> /dev/null && {
 24                 userdel -r $USERNAME
 25                 echo " $USERNAME 删除成功!"
 26                 echo "#########################################"
 27                         }||{
 28                 echo -e "\033[31m $USERNAME 不存在!\033[0m"
 29                 }
 30                 ;;
 31                 E)
 32                 echo "退出!"
 33                 echo "#########################################"
 34                 exit
 35                 ;;
 36                 *)
 37                 echo -e "\033[31m 输入错误!\033[0m"
 38         esac
 39 done

效果:
在这里插入图片描述

展开阅读全文

没有更多推荐了,返回首页