创建文本菜单创建菜单布局
root@wl-MS-7673:/home/wl/桌面/shell# cat -n test1.sh
1 #!/bin/bash
2 echo -e "\t\tMenu"
3 echo -e "1.\tDisplay disk space"
4 echo -e "2.\tDisplay logged on user"
5 echo -e "3.\tDisplay memory usage"
6 echo -e "0.\tExit menu\n\n"
7 echo -en "\t\tEnter option:" #-n表示不打印回车
8 read -n1 option #读取一个字符
9 echo ""
root@wl-MS-7673:/home/wl/桌面/shell# ./test1.sh
Menu
1. Display disk space
2. Display logged on user
3. Display memory usage
0. Exit menu
Enter option:1
root@wl-MS-7673:/home/wl/桌面/shell#
创建菜单函数
只需把上面的代码用函数包起来即可
添加菜单逻辑添加对应的case即可
root@wl-MS-7673:/home/wl/桌面/shell# cat -n test2.sh
1 #!/bin/bash
2 option=""
3 create_menu(){
4 echo -e "\t\tMenu"
5 echo -e "1.\tDisplay disk space"
6 echo -e "2.\tDisplay logged on user"
7 echo -e "3.\tDisplay memory usage"
8 echo -e "0.\tExit menu\n\n"
9 echo -en "\t\tEnter option:"
10 read -n1 option
11 echo ""
12 }
13
14 create_menu
15
16 case $option in
17 0)
18 echo "Display disk space";;
19 1)
20 echo "Display logged on user";;
21 2)
22 echo "Display memory usage";;
23 3)
24 echo "Exit menu";;
25 *)
26 echo "error";;
27 esac
28
root@wl-MS-7673:/home/wl/桌面/shell# ./test2.sh
Menu
1. Display disk space
2. Display logged on user
3. Display memory usage
0. Exit menu
Enter option:3
Exit menu
root@wl-MS-7673:/home/wl/桌面/shell# apt-get install sed
root@wl-MS-7673:/home/wl/桌面/shell# cat test3.sh
#/bin/bash
ption=""
create_menu(){
echo -e "\t\tMenu"
echo -e "1.\tDisplay disk space"
echo -e "2.\tDisplay logged on user"
echo -e "3.\tDisplay memory usage"
echo -e "0.\tExit menu\n\n"
echo -en "\t\tEnter option:"
read -n1 option
echo ""
}
create_menu
disk_space(){
df -k
}
whoseon(){
who
}
menu_usage(){
cat /proc/meminfo
}
deal_menu(){
case $option in
1)
disk_space;;
2)
whoseon;;
3)
menu_usage;;
0)
echo "Exit menu";;
*) #输入错误,重新输入
echo -e "\nSorry, wrong selection."
echo -en "\n\n\t\tHit any key to continue."
read -n1 option #重新读取菜单选项
deal_menu;;
esac
}
deal_menu
root@wl-MS-7673:/home/wl/桌面/shell#
运行结果如图:
root@wl-MS-7673:/home/wl/桌面/shell# ./test3.sh
Menu
1. Display disk space
2. Display logged on user
3. Display memory usage
0. Exit menu
Enter option:
root@wl-MS-7673:/home/wl/桌面/shell# ./test3.sh
Menu
1. Display disk space
2. Display logged on user
3. Display memory usage
0. Exit menu
Enter option:1
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda11 68151872 7654680 57035204 12% /
udev 2039652 4 2039648 1% /dev
tmpfs 818784 908 817876 1% /run
none 5120 0 5120 0% /run/lock
none 2046956 264 2046692 1% /run/shm
root@wl-MS-7673:/home/wl/桌面/shell#
使用select命令
root@wl-MS-7673:/home/wl/桌面/shell# cat -n test4.sh
1 #!/bin/bash
2
3
4 disk_space(){
5
6 df -k
7 }
8
9 whoseon(){
10
11 who
12 }
13
14 menu_usage(){
15
16 cat /proc/meminfo
17 }
18
19
20
21 PS3="Enter option:"
22 select option in "Display disk space" "Display logged on user" "Display memory usage" "Exit menu"
23 do
24 case $option in
25 "Display disk space")
26 disk_space;;
27 "Display logged on user")
28 whoseon;;
29 "Display memory usage")
30 menu_usage;;
31 "Exit menu")
32 echo "Exit menu"
33 break;;
34 *)
35 echo -e "\nSorry, wrong selection."
36 echo -en "\n\n\t\tHit any key to continue."
37 read -n1 option
38 deal_menu;;
39 esac
40 done
root@wl-MS-7673:/home/wl/桌面/shell#
运行结果如图所示:
root@wl-MS-7673:/home/wl/桌面/shell# ./test4.sh
1) Display disk space 3) Display memory usage
2) Display logged on user 4) Exit menu
Enter option:1
文件系统 1K-块 已用 可用 已用% 挂载点
/dev/sda11 68151872 7654632 57035252 12% /
udev 2039652 4 2039648 1% /dev
tmpfs 818784 908 817876 1% /run
none 5120 0 5120 0% /run/lock
none 2046956 264 2046692 1% /run/shm
Enter option:2
wl pts/0 2013-11-25 15:44 (:0.0)
wl pts/1 2013-11-25 17:31 (:0.0)
Enter option:3
MemTotal: 4093912 kB
MemFree: 2341120 kB
Buffers: 187220 kB
Cached: 702080 kB
SwapCached: 0 kB
Active: 917904 kB
Inactive: 521664 kB
Active(anon): 551016 kB
Inactive(anon): 2088 kB
Active(file): 366888 kB
Inactive(file): 519576 kB
Unevictable: 16 kB
Mlocked: 16 kB
HighTotal: 3253080 kB
HighFree: 1780332 kB
LowTotal: 840832 kB
LowFree: 560788 kB
SwapTotal: 4165628 kB
SwapFree: 4165628 kB
Dirty: 92 kB
Writeback: 0 kB
AnonPages: 550372 kB
Mapped: 218440 kB
Shmem: 2840 kB
Slab: 66112 kB
SReclaimable: 47724 kB
SUnreclaim: 18388 kB
KernelStack: 3488 kB
PageTables: 8988 kB
NFS_Unstable: 0 kB
Bounce: 0 kB
WritebackTmp: 0 kB
CommitLimit: 6212584 kB
Committed_AS: 2815796 kB
VmallocTotal: 122880 kB
VmallocUsed: 30336 kB
VmallocChunk: 92168 kB
HardwareCorrupted: 0 kB
AnonHugePages: 0 kB
HugePages_Total: 0
HugePages_Free: 0
HugePages_Rsvd: 0
HugePages_Surp: 0
Hugepagesize: 2048 kB
DirectMap4k: 8184 kB
DirectMap2M: 905216 kB
Enter option:4
Exit menu
root@wl-MS-7673:/home/wl/桌面/shell#
在脚本中使用dialog命令
root@wl-MS-7673:/home/wl/桌面/shell# cat -n test5.sh
1 #!/bin/bash
2 temp=`mktemp -t temp.XXXXXX`
3 temp2=`mktemp -t temp2.XXXXXX`
4 disk_space(){
5 df -k > $temp
6 dialog --textbox $temp 20 60
7 }
8
9 whoseon(){
10 who > $temp
11 dialog --textbox $temp 20 50
12 }
13
14 menu_usage(){
15 cat /proc/meminfo > $temp
16 dialog --textbox $temp 20 50
17 }
18
19 dialog --menu "menu" 20 30 10 1 "Display disk space" 2 "Display logged on user" 3 "Display memory usage" 0 "Exit menu" 2>$temp2
20 if [ $? -ne 1 ]
21 then
22 selection=`cat $temp2`
23 case $selection in
24 1)
25 disk_space;;
26 2)
27 whoseon;;
28 3)
29 menu_usage;;
30 0) ;;
31 *)
32 dialog --msgbox "Sorry, invalid selection" 10 30
33 esac
34 fi
35
36 rm -f $temp $temp2 2>/dev/null
root@wl-MS-7673:/home/wl/桌面/shell#
运行结果与上述类似。
zenity --calendar
root@wl-MS-7673:/home/wl/桌面/shell# cat -n test6.sh
1 #!/bin/bash
2 temp=`mktemp -t temp.XXXXXX`
3 temp2=`mktemp -t temp2.XXXXXX`
4 disk_space(){
5 df -k > $temp
6 zenity --text-info --title "Disk space" --filename=$temp --width 750 --height 300
7 }
8
9 whoseon(){
10 who > $temp
11 zenity --text-info --title "Logged on user" --filename=$temp --width 500 --height 200
12 }
13
14 menu_usage(){
15 cat /proc/meminfo > $temp
16 zenity --text-info --title "Memory usage" --filename=$temp --width 300 --height 500
17 }
18
19 zenity --list --radiolist --title "Menu" --column "Select" \
20 --column "Menu Item" FALSE "Display disk space" FALSE "Display logged on user" FALSE "Display memory usage" FALSE "Exit" > $temp2
21 if [ $? -ne 1 ]
22 then
23 selection=`cat $temp2`
24 case $selection in
25 "Display disk space")
26 disk_space;;
27 "Display logged on user")
28 whoseon;;
29 "Display memory usage")
30 menu_usage;;
31 "Exit") ;;
32 *)
33 zenity --info "Sorry. invalid selection."
34 esac
35 fi
36
37 rm -f $temp $temp2 2>/dev/null
root@wl-MS-7673:/home/wl/桌面/shell#