菜单提示

一段风格良好的带菜单提示的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
# Script to create simple menus and take action according to that selected
# menu item
#
while :
do
clear
echo "-------------------------------------"
echo " Main Menu "
echo "-------------------------------------"
echo "[1] Show Todays date/time"
echo "[2] Show files in current directory"
echo "[3] Show calendar"
echo "[4] Start editor to write letters"
echo "[5] Exit/Stop"
echo "======================="
echo -n "Enter your menu choice [1-5]: "
read yourch
case $yourch in
1) echo "Today is `date` , press a key. . ." ; read ;;
2) echo "Files in `pwd`" ; ls -l ; echo "Press a key. . ." ; read ;;
3) cal ; echo "Press a key. . ." ; read ;;
4) vi ;;
5) exit 0 ;;
*) echo "Opps!!! Please select choice 1,2,3,4, or 5";
echo "Press a key. . ." ; read ;;
esac
done

对话框组件

RedHat 提供了一系列对话框组件。可以创建出更为美观的对话框。

所有可用的 box 类型包括:

  • buildlist :列表对话框;
  • calendar :日历对话框;
  • checklist :复选列表对话框;
  • radiolist :单选列表对话框;
  • dselect :目录选择对话框;
  • fselect :文件选择对话框;
  • editbox :编辑对话框框;
  • form :表单对话框;
  • gauge :刻度对话框(用于显示进度);
  • infobox :消息对话框;
  • inputbox :输入对话框;
  • menu :菜单对话框;
  • msgbox :带“Ok”按钮的消息对话框;
  • yesnobox : 带 “yes no”按钮的消息对话框
  • inputmenu :带输入功能的菜单;
  • mixedform :混合表单;
  • mixedgauge :混合刻度对话框;
  • pause :带刻度和“Ok”、“Cancel”按钮的对话框;
  • passwordbox :密码输入对话框;
  • passwordform :密码输入表单;
  • prgbox : 可编程对话框,用于展示一个命令的输出结果,和 programbox很接近;
  • programbox : 带“Ok”按钮的progressbox;
  • progressbox : 显示一个命令的管道输出;
  • rangebox : 范围对话框。允许用户选取一个范围内的值。
  • tailboxbg : 在对话框中展示一个文本,使用tail -f &命令作为后台任务;
  • textbox :在对话框中展示文本;
  • timebox :展示时间(时、分、秒)的对话框;
  • treeview : 用树的形式展示数据。

用法:

1
2
3
4
5
6
7
8
dialog --title {标题} --backtitle {左上角的标题} {Box options}
Box options 包括:
--yesno {text} {height} {width}
--msgbox {text} {height} {width}
--infobox {text} {height} {width}
--inputbox {text} {height} {width} [{init}]
--textbox {file} {height} {width}
--menu {text} {height} {width} {menu-height} [ tag item ] ...

Info Box

1
2
3
4
dialog --title "Linux Dialog Utility Infobox" --backtitle "Linux Shell Script\
Tutorial" --infobox "This is dialog box called infobox, which is used \
to show some information on screen, Thanks to Savio Lam and\
Stuart Herbert to give us this utility. Press any key. . . " 7 50 ; read

Message Box

1
2
3
4
dialog --title "Linux Dialog Utility Msgbox" --backtitle "Linux Shell Script\
Tutorial" --msgbox "This is dialog box called msgbox, which is used\
to show some information on screen which has also Ok button, Thanks to Savio Lam\
and Stuart Herbert to give us this utility. Press any key. . . " 9 50

yesno Box

1
2
3
4
5
6
7
8
9
dialog --title "Alert : Delete File" --backtitle "Linux Shell Script\
Tutorial" --yesno "\nDo you want to delete '/usr/letters/jobapplication'\
file" 7 60
sel=$? # 获取对话框组件的 exit 返回值
case $sel in
0) echo "User select to delete file";;
1) echo "User select not to delete file";;
255) echo "Canceled by user by pressing [ESC] key";;
esac

Input Box

1
2
3
4
5
6
7
8
9
10
11
12
13
dialog --title "Inputbox - To take input from you" --backtitle "Linux Shell\
Script Tutorial" --inputbox "Enter your name please" 8 60 2>/tmp/input.$$
sel=$?
na=`cat /tmp/input.$$`
case $sel in
0) echo "Hello $na" ;;
1) echo "Cancel is Press" ;;
255) echo "[ESCAPE] key pressed" ;;
esac
rm -f /tmp/input.$$

更美观的菜单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
#How to create small menu using dialog
#
dialog --backtitle "Linux Shell Script Tutorial " --title "Main\
Menu" --menu "Move using [UP] [DOWN],[Enter] to\
Select" 15 50 3\
Date/time "Shows Date and Time"\
Calendar "To see calendar "\
Editor "To start vi editor " 2>/tmp/menuitem.$$
menuitem=`cat /tmp/menuitem.$$`
opt=$?
case $menuitem in
Date/time) date;;
Calendar) cal;;
Editor) vi;;
esac