Shell脚本编程(二), Shell编写menu菜单选项

1·menu.sh文件

#!/bin/bash
#menu.sh
function menu(){
title="My Menu"
url="www.mobanw.com"
time=`date +%Y-%m-%d`

cat << eof
###################################################
            `echo -e "\033[32;40m$title\033[0m"`
###################################################
*  1)add a user
*  2)set password for user
*  3)delete a user
*  4)print disk space
*  5)print mem space
*  6)quit
*  7)return main menu
###################################################
$url                 $time
###################################################
eof
}

2·index.sh

#!/bin/bash
. menu.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:" name
            read -p "set pass for the user:" pass
            echo $pass | passwd --stdin $name &>/dev/null
            if [ $? -eq 0 ] ; then
                str="${name}'s password is set Successfully!!!"
                echo -e "\033[30;47m$str\033[0m"
            else
                str="${name}'s password is set Failly!!!"
                echo -e "\033[31;47m$str\033[0m"
            fi
            ;;
        3)
            read -p "delete a user:" name
            userdel -r $name>/dev/null
            if [ $? -eq 0 ] ; then
                str="user ${name} is deleted Successfully!!!"
                echo -e "\033[30;47m$str\033[0m"
            else
                str="user ${name} is deleted Failly!!!"
                echo -e "\033[31;47m$str\033[0m"
            fi
            ;;
        4)
            str=`free -m`
            echo -e "\033[32;47m$str\033[0m"
            ;;
        5)
            str=`df -Th`
            echo -e "\033[32;47m$str\033[0m"
            ;;
        6)
            echo -e "\033[32;47mQuit Success!!!\033[0m"
            break
            ;;
        7)
            clear
            menu
            ;;
    esac
done
0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!
发表我的评论

Hi,请填写昵称和邮箱!

取消评论
代码 贴图 加粗 链接 删除线 签到