shell 字符菜单管理

1、创建一个脚本func.sh

脚本如下func2.sh

#!/bin/bash

function menu(){
title="My Menu"
url="www.lampym.com"
time=`date +%Y-%m-%d`
cat << eof

########################################
        $title
########################################
*    1)add a user
*    2)set password for user
*    3)delete e user
*    4)print disk sapce
*    5)print mem space
*    6)quit
########################################
$url              $time
########################################
eof
}
func2.sh

2、创建脚本index.sh

#!/bin/bash

. func2.sh

clear
menu

while true
do    
     read -p "please input a option:" option
     case $option in
        1) 
          read -p "add a user:" name
          useradd $name  > /dev/null
          if [ $? -eq 0 ] ;then
            str="user ${name} is created successfully"
            echo -e "\033[30;47m$str\033[0m"
          else
            str="user ${name} is created failly!"
            echo -e "\033[31;47m$str\033[0m"
          fi;;
        2) 
         read -p "input the user:" user
         read -p "set password for the user:" passwd
         echo $passwd | passwd --stdin $user > /dev/null
         if [ $? -eq 0 ] ;then
            str="${user}'s password set successfully!"
            echo -e "\033[30;47m$str\033[0m"
         else
            str="${user}'s password set failly"
            echo -e "\033[31;47m$str\033[0m"
            
         fi;;
        3) 
             read -p "input a user:" user
         userdel -r $user
         if  [ $? -eq 0 ] ;then
            str="$user delete successfully!"
            echo -e "\033[30;47m$str\033[0m"
         else
            str="$user delete failly"
            echo -e "\033[31;47m$str\033[0m"
         fi
            ;;
        4) echo "你选择了第四个选项";;
        5) echo "你选择了第五个选项";;
        6) echo "你选择了退出!"
          break    ;;
        7) clear
           menu
      esac
done
index.sh

 

posted @ 2019-06-20 23:43  北极星0202  阅读(228)  评论(0编辑  收藏  举报