shell菜单编程脚本实例
时间:2014-11-02 02:26 来源:linux.it.net.cn 作者:it
例子,shell菜单脚本代码。
复制代码代码示例:
#!/bin/bash
#f_menu
xy()
{
_R=$1;
_C=$2;
tput cup $_R $_C
}
colour()
{
case $1 in
black_green)
echo -e "\033[40;32m"
;;
black_yellow)
echo -e "\033[40;33m"
;;
black_white)
echo -e "\033[40;37m"
;;
black_cyan)
echo -e "\033[40;36m"
;;
black_blue)
echo -e "\033[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 "\t\tUnknow user response\n"
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 "\t\tHit the return key to continue"
read J
clear
done
代码解释:
重点:使用<<!
.....
!
创建菜单
case .....esac
if条件
then动作
elif 条件
then动作
else
动作
fi
while 条件
do
动作
done
(责任编辑:IT)
例子,shell菜单脚本代码。
复制代码代码示例:
#!/bin/bash
#f_menu xy() { _R=$1; _C=$2; tput cup $_R $_C } colour() { case $1 in black_green) echo -e "\033[40;32m" ;; black_yellow) echo -e "\033[40;33m" ;; black_white) echo -e "\033[40;37m" ;; black_cyan) echo -e "\033[40;36m" ;; black_blue) echo -e "\033[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 "\t\tUnknow user response\n" 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 "\t\tHit the return key to continue" read J clear done
代码解释: |