很多人误以为shell脚本只能在命令行下使用。其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等。你可以控制最终的输出,光标位置还有各种输出效果。下面我将介绍一些工具,帮助你创建强大的,互动的,用户友好的 Unix/Linux shell脚本。我在FreeBSD和Linux下测试过这些工具,不过其他UNIX系列的操作系统应该都支持的。
1. notify-send 命令
这个命令可以让你通过通知进程发送一个桌面通知给用户。这可以用来向用户发送提示,或者显示一些信息而不用打断用户工作。你需要安装如下软件包:
1. $ sudo apt-get install libnotify-bin
2.
下面这个例子展示了如何从命令行向桌面发送一个简单的消息:
1. notify-send "rsnapshot done :)"
2.
输出:
下面是一个复杂一点的例子:
1. ....
2. alert=18000
3. live=$(lynx --dump http://money.rediff.com/ | grep 'BSE LIVE' | awk '{ print $5}' | sed 's/,//g;s/\.[0-9]*//g')
4. notify_counter=1; }
5. ...
输出:
这里的参数解释如下:
- -t 5000:指定超时的时间,毫秒
- -u low:设置是否紧急
- -i gtk-dialog-info:通知图标,你可以指定图标 -i /path/to/your-icon.png
2. tput 命令
这个命令是用来设置终端特性的:
- 移动光标
- 获得终端信息
- 设置前景和背景色
- 设置粗体模式
- 设置反模式等等
举例:
1. 01 #!/bin/bash
2.
3. 02
4.
5. 03 # clear the screen
6.
7. 04 tput clear
8.
9. 05
10.
11. 06 # Move cursor to screen location X,Y (top left is 0,0)
12.
13. 07 tput cup 3 15
14.
15. 08
16.
17. 09 # Set a foreground colour using ANSI escape
18.
19. 10 tput setaf 3
20.
21. 11 echo "XYX Corp LTD."
22.
23. 12 tput sgr0
24.
25. 13
26.
27. 14 tput cup 5 17
28.
29. 15 # Set reverse video mode
30.
31. 16 tput rev
32.
33. 17 echo "M A I N - M E N U"
34.
35. 18 tput sgr0
36.
37. 19
38.
39. 20 tput cup 7 15
40.
41. 21 echo "1. User Management"
42.
43. 22
44.
45. 23 tput cup 8 15
46.
47. 24 echo "2. Service Management"
48.
49. 25
50.
51. 26 tput cup 9 15
52.
53. 27 echo "3. Process Management"
54.
55. 28
56.
57. 29 tput cup 10 15
58.
59. 30 echo "4. Backup"
60.
61. 31
62.
63. 32 # Set bold mode
64.
65. 33 tput bold
66.
67. 34 tput cup 12 15
68.
69. 35 read -p "Enter your choice [1-4] " choice
70.
71. 36
72.
73. 37 tput clear
74.
75. 38 tput sgr0
76.
77. 39 tput rc
78.
输出:
3. setleds 命令
这个命令可以让你控制键盘灯,例如打开数字键盘灯:
1. setleds -D +num
2.
关闭数字键盘灯:
1. setleds -D -num
2.
-caps: 清除大写灯
- +caps:打开大写灯
- -scroll:清除滚动锁
- +scroll:打开滚动锁
4. zenity 命令
这个命令可以显示GTK+的对话框,然后返回用户的输入。你可以用这个命令在脚本中显示信息,并要求用户输入信息。下面这段代码就是域名的whois查询:
1. 01 #!/bin/bash
2.
3. 02 # Get domain name
4.
5. 03 _zenity="/usr/bin/zenity"
6.
7. 04 _out="/tmp/whois.output.$$"
8.
9. 05 domain=$(${_zenity} --title "Enter domain" \
10.
11. 06 --entry --text "Enter the domain you would like to see whois info" )
12.
13. 07
14.
15. 08 if [ $? -eq 0 ]
16.
17. 09 then
18.
19. 10 # Display a progress dialog while searching whois database
20.
21. 11 whois $domain | tee >(${_zenity} --width=200 --height=100
22.
23. 12 --title="whois"
24.
25. 13 --pulsate --text="Searching domain info..."
26.
27. 14 --auto-kill --auto-close \
28.
29. 15 --percentage=10) >${_out}
30.
31. 16
32.
33. 17 # Display back output
34.
35. 18 ${_zenity} --width=800 --height=600
36.
37. 19 --title "Whois info for $domain" \
38.
39. 20 --text-info --filename="${_out}"
40.
41. 21 else
42.
43. 22 ${_zenity} --error \
44.
45. 23 --text="No input provided"
46.
47. 24 fi
48.
输出:
5. kdialog 命令
这个命令和zenity很想,只不过它是为KDE/QT应用准备的。使用方法如下:
1. kdialog --dontagain myscript:nofilemsg --msgbox "File: '~/.backup/config' not found."
2.
输出
你可以查看 shell scription with KDE Dialogs 来获取更多信息
6. Dialog
这个命令可以在shell脚本中显示文本组件。它使用了curses和ncurses类库。示例代码:
1. 01 >#!/bin/bash
2.
3. 02 dialog --title "Delete file" \
4.
5. 03 --backtitle "Linux Shell Script Tutorial Example" \
6.
7. 04 --yesno "Are you sure you want to permanently delete \"/tmp/foo.txt\"?" 7 60
8.
9. 05
10.
11. 06 # Get exit status
12.
13. 07 # 0 means user hit [yes] button.
14.
15. 08 # 1 means user hit [no] button.
16.
17. 09 # 255 means user hit [Esc] key.
18.
19. 10 response=$?
20.
21. 11 case $response in
22.
23. 12 0) echo "File deleted.";;
24.
25. 13 1) echo "File not deleted.";;
26.
27. 14 255) echo "[ESC] key pressed.";;
28.
29. 15 esac
30.
7. logger 命令
这个命令可以让你写入系统日志例如 /var/log/messages:
1. 1 logger "MySQL database backup failed."
2.
3. 2 tail -f /var/log/messages
4.
5. 3 logger -t mysqld -p daemon.error "Database Server failed"
6.
7. 4 tail -f /var/log/syslog
输出:
1. Apr 20 00:11:45 vivek-desktop kernel: [38600.515354] CPU0: Temperature/speed normal
2. Apr 20 00:12:20 vivek-desktop mysqld: Database Server failed
8. setterm 命令
这个命令可以设置中断的属性。下面的例子是强制屏幕全黑15分钟,并且60分钟后把显示器设为待机状态:
1. setterm -blank 15 -powersave powerdown -powerdown 60
2.
下面这段命令可以在中断显示加下划线的文字:
1. setterm -underline on;
2.
3. echo "Add Your Important Message Here"
4.
5. setterm -underline off
6.
或者你可以关闭光标:
1. setterm -cursor off
2.
9. smbclient:向 MS-Windows 系统发送消息
smbclient可以和 SMB/CIFS服务器通信。它可以向MS-Windows系统的指定用户发送消息:
1. 1 smbclient -M WinXPPro <<EOF
2.
3. 2 Message 1
4.
5. 3 Message 2
6.
7. 4 ...
8.
9. 5 ..
10.
11. 6 EOF
12.
或者
1. 1 echo "${Message}" | smbclient -M salesguy2
2.
10. Bash Socket 编程
你可以在bash中开启一个socket链接,并且传输数据。Bash有两个特殊的设备文件:
- /dev/tcp/host/port - 如果hostname,和port是合法的话,bash会尝试开启一个TCP连接。
- /dev/udp/host/port - 如果hostname和port是合法的话,bash会开启一个UDP连接。
你可以利用这个技术来测试一台主机的端口是否是开启的,而不需要使用nmap或者port扫描器:
1. # find out if TCP port 25 open or not
2.
3. (echo >/dev/tcp/localhost/25) &>/dev/null && echo "TCP port 25 open" || echo "TCP port 25 close"
4.
你可以 使用循环来查找开着的端口:
1. 1 echo "Scanning TCP ports..."
2.
3. 2 for p in {1..1023}
4.
5. 3 do
6.
7. 4 (echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open"
8.
9. 5 done
10.
输出:
Scanning TCP ports...
22 open
53 open
80 open
139 open
445 open
631 open下面的这个例子让你的脚本扮演HTTP客户端:
1. 01 #!/bin/bash
2.
3. 02 exec 3<> /dev/tcp/${1:-www.cyberciti.biz}/80
4.
5. 03
6.
7. 04 printf "GET / HTTP/1.0\r\n" >&3
8.
9. 05 printf "Accept: text/html, text/plain\r\n" >&3
10.
11. 06 printf "Accept-Language: en\r\n" >&3
12.
13. 07 printf "User-Agent: nixCraft_BashScript v.%s\r\n" "${BASH_VERSION}" >&3
14.
15. 08 printf "\r\n" >&3
16.
17. 09
18.
19. 10 while read LINE <&3
20.
21. 11 do
22.
23. 12 # do something on $LINE
24.
25. 13 # or send $LINE to grep or awk for grabbing data
26.
27. 14 # or simply display back data with echo command
28.
29. 15 echo $LINE
30.
31. 16 done
32.
关于GUITools和Cronjob
如果你使用cronjob来调用你的脚本的话,你要通过“ export DISPLAY=[user's machine]:0 ”命令来设置本地的 display/input 服务。例如调用 /home/vivek/scripts/monitor.stock.sh脚本,它使用了 zenity 工具:
1. 1 @hourly DISPLAY=:0.0 /home/vivek/scripts/monitor.stock.sh
2.
所有的命令你都可以通过“man”来查询详细的使用方式。
















