有时候脚本程序需要简单的界面让用户做些简单交互,比如输入和选择等,然后去执行相应的功能,比如如下界面:
用户可以输入数字加回车,来选择执行不同的任务,代码实现如下:
clear
echo
echo
echo
echo -e "\033[47;34m*****************************************************\033[0m"
echo -e "\033[47;34m*\033[0m Welcome to use xxxx Controller \033[47;34m*\033[0m"
echo -e "\033[47;34m*\033[0m ________V2.2HKJV \033[47;34m*\033[0m"
echo -e "\033[47;34m*****************************************************\033[0m"
echo " "
echo " Please select function number"
echo -e "\t\033[46;38m1.\033[0m Stop xx program\033[0m"
echo -e "\t\033[46;38m2.\033[0m Start / Restart program\033[0m"
echo -e "\t\033[46;38m3.\033[0m Show Current info \033[0m"
echo -e "\t\033[46;38m4.\033[0m Change settings\033[0m"
echo -e "\t\033[46;38m5.\033[0m Inquiry settins \033[0m"
echo -e "\t\033[46;38m6.\033[0m Trace Logs\033[0m"
echo -e "\t\033[46;38m7.\033[0m Check status\033[0m"
echo -e "\t\033[46;38m8.\033[0m Navigate to working directory\033[0m"
bLoop=true
-e是echo的拓展显示,包括字体颜等,这个后面会另写一文把常用的列出来。
bLoop是用来控制用户选用完一个功能后是否继续此程序,(再次调用读取用户输入命令),还是退出。
读取用户输入的循环:
while $bLoop
do
echo
echo -e " \033[45;39mPlease input 0-8(0 for Exit):\033[0m"
read cmdid
case $cmdid in
0)echo "See You..."
bLoop=false
;;
1)echo "Stop Apps..."
/home/cc.sh
;;
2)RestartApp
bLoop=false
;;
3)echo "The information retrieved from r_xx.ini is:"
grep stationid /home/hm/ini/r_xx.ini
;;
4)read -p "Please input new id:" stnid
bLoop=true
Changeid $stnid
;;
5)read -p "Please input any Information of ID :" stnInfo
grep -i "$stnInfo" xsltje.txt #find rows include input info case insensitively
;;
6)TraceLogs
bLoop=false
;;
7)ps -ef|grep myproPrefix|grep -v grep
bLoop=false
;;
8) bLoop=false
cd /home/workspace/
exec /bin/bash
;;
*)echo "Please Input 0-8"
;;
esac
done
RestartApp,Changeid 等子程序要在前面先定义好。
————————————————
需要500关注,麻烦顺手点个关注,谢谢