shell菜单编程脚本实例

发布时间:2020-10-01编辑:脚本学堂
分享一个shell菜单的实现脚本,学习下shell编程中是如何实现菜单功能的,需要的朋友参考下。
例子,shell菜单脚本代码。
复制代码 代码示例:
#!/bin/bash
#f_menu
xy()
{
_R=$1;
_C=$2;
tput cup $_R $_C
}
colour()
{
case $1 in
black_green)
echo -e "33[40;32m"
;;
black_yellow)
echo -e "33[40;33m"
;;
black_white)
echo -e "33[40;37m"
;;
black_cyan)
echo -e "33[40;36m"
;;
black_blue)
echo -e "33[40;34m"
;;
esac
}
trap "" 1 2 3
mday=`date +%d/%m/%y`
mhost=`hostname`
mwho=`whoami`
while :
do
cat <<mmenu
      ----------------------------------------------------------------
         $mwho          $mhost          $mday
      ----------------------------------------------------------------
                1: change colour
                2: use the vi
                3: see who on the system
                H: help
                Q: quit
      ----------------------------------------------------------------
mmenu
echo -e -n "tEnter Your Choice[1,2,3,H,Q]:"
read Cho
case $Cho in
1)
while :
do
cat <<kcol
      ----------------------------------------------------------------
         $mwho          $mhost          $mday
      ----------------------------------------------------------------
        1:black_white   2:black_yellow  3:black_green
        4:black_blue    5:black_cyan    6:return pre_menu
     -----------------------------------------------------------------
kcol
echo -e -n "t Enter Your Choice[1,2,3,4,5,6]:"
read choice
        if [ "$choice" = "1" ]
        then
        colour black_white
        elif [ "$choice" = "2" ]
        then
        colour black_yellow
        elif [ "$choice" = "3" ]
        then
        colour black_green
        elif [ "$choice" = "4" ]
        then
        colour black_blue
        elif [ "$choice" = "5" ]
        then
        colour black_cyan
        elif [ "$choice" = "6" ]
        then
        break
        else
        echo -e -n "ttUnknow user responsen"
        fi
clear
done
;;
2)
vi
;;
3)
who
;;
H|h)
cat <<mmenu
this is the help screen ,nothing here yet to help you!
mmenu
;;
Q|q)
exit 0
;;
*)
echo -e "tUnknow user response"
;;
esac
echo -e -n "ttHit the return key to continue"
read J
clear
done

代码解释:
重点:使用<<!
.....
!
创建菜单
case .....esac
 
if条件
then动作
elif 条件
then动作
 else
动作
fi
while 条件
do
动作
done