很多人誤以爲shell腳本只能在命令行下使用。其實shell也可以調用一些GUI組件,例如菜單,警告框,進度條等等。你可以控制最終的輸出,光標位置還有各種輸出效果。下面我將介紹一些工具,幫助你創建強大的,互動的,用戶友好的 Unix/Linux shell腳本。我在FreeBSD和Linux下測試過這些工具,不過其他UNIX系列的操作系統應該都支持的。
1. notify-send 命令
這個命令可以讓你通過通知進程發送一個桌面通知給用戶。這可以用來向用戶發送提示,或者顯示一些信息而不用打斷用戶工作。你需要安裝如下軟件包:
1 |
$ sudo apt-get install libnotify-bin |
下面這個例子展示瞭如何從命令行向桌面發送一個簡單的消息:
1 |
notify-send "rsnapshot done :)" |
輸出:
下面是一個複雜一點的例子:
3 |
live=$(lynx --dump http://money.rediff.com/ | grep 'BSE LIVE' | awk '{ print $5}' | sed 's/,//g;s/\.[0-9]*//g') |
4 |
[ $notify_counter -eq 0 ] && [ $live -ge $alert ] && { notify-send -t 5000 -u low -i "BSE Sensex touched 18k"; notify_counter=1; } |
輸出:
這裏的參數解釋如下:
- -t 5000:指定超時的時間,毫秒
- -u low:設置是否緊急
- -i gtk-dialog-info:通知圖標,你可以指定圖標 -i /path/to/your-icon.png
2. tput 命令
這個命令是用來設置終端特性的:
- 移動光標
- 獲得終端信息
- 設置前景和背景色
- 設置粗體模式
- 設置反模式等等
舉例:
06 |
# Move cursor to screen location X,Y (top left is 0,0) |
09 |
# Set a foreground colour using ANSI escape |
15 |
# Set reverse video mode |
17 |
echo "M A I N - M E N U" |
21 |
echo "1. User Management" |
24 |
echo "2. Service Management" |
27 |
echo "3. Process Management" |
35 |
read -p "Enter your choice [1-4] " choice |
輸出:
3. setleds 命令
這個命令可以讓你控制鍵盤燈,例如打開數字鍵盤燈:
關閉數字鍵盤燈:
- -caps: 清除大寫燈
- +caps:打開大寫燈
- -scroll:清除滾動鎖
- +scroll:打開滾動鎖
4. zenity 命令
這個命令可以顯示GTK+的對話框,然後返回用戶的輸入。你可以用這個命令在腳本中顯示信息,並要求用戶輸入信息。下面這段代碼就是域名的whois查詢:
03 |
_zenity="/usr/bin/zenity" |
04 |
_out="/tmp/whois.output.$$" |
05 |
domain=$(${_zenity} --title "Enter domain" \ |
06 |
--entry --text "Enter the domain you would like to see whois info" ) |
10 |
# Display a progress dialog while searching whois database |
11 |
whois $domain | tee >(${_zenity} --width=200 --height=100 \ |
12 |
--title="whois" --progress \ |
13 |
--pulsate --text="Searching domain info..." \ |
14 |
--auto-kill --auto-close \ |
15 |
--percentage=10) >${_out} |
18 |
${_zenity} --width=800 --height=600 \ |
19 |
--title "Whois info for $domain" \ |
20 |
--text-info --filename="${_out}" |
23 |
--text="No input provided" |
輸出:
5. kdialog 命令
這個命令和zenity很想,只不過它是爲KDE/QT應用準備的。使用方法如下:
1 |
kdialog --dontagain myscript:nofilemsg --msgbox "File: '~/.backup/config' not found." |
輸出
你可以查看 shell scription with KDE Dialogs 來獲取更多信息
6. Dialog
這個命令可以在shell腳本中顯示文本組件。它使用了curses和ncurses類庫。示例代碼:
02 |
dialog --title "Delete file" \ |
03 |
--backtitle "Linux Shell Script Tutorial Example" \ |
04 |
--yesno "Are you sure you want to permanently delete \"/tmp/foo.txt\"?" 7 60 |
07 |
# 0 means user hit [yes] button. |
08 |
# 1 means user hit [no] button. |
09 |
# 255 means user hit [Esc] key. |
12 |
0) echo "File deleted.";; |
13 |
1) echo "File not deleted.";; |
14 |
255) echo "[ESC] key pressed.";; |
7. logger 命令
這個命令可以讓你寫入系統日誌例如 /var/log/messages:
1 |
logger "MySQL database backup failed." |
2 |
tail -f /var/log/messages |
3 |
logger -t mysqld -p daemon.error "Database Server failed" |
4 |
tail -f /var/log/syslog |
輸出:
Apr 20 00:11:45 vivek-desktop kernel: [38600.515354] CPU0: Temperature/speed normal
Apr 20 00:12:20 vivek-desktop mysqld: Database Server failed
8. setterm 命令
這個命令可以設置中斷的屬性。下面的例子是強制屏幕全黑15分鐘,並且60分鐘後把顯示器設爲待機狀態:
1 |
setterm -blank 15 -powersave powerdown -powerdown 60 |
下面這段命令可以在中斷顯示加下劃線的文字:
2 |
echo "Add Your Important Message Here" |
或者你可以關閉光標:
9. smbclient:向 MS-Windows 系統發送消息
smbclient可以和 SMB/CIFS服務器通信。它可以向MS-Windows系統的指定用戶發送消息:
1 |
smbclient -M WinXPPro <<EOF |
或者
1 |
echo "${Message}" | smbclient -M salesguy2 |
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 |
(echo >/dev/tcp/localhost/25) &>/dev/null && echo "TCP port 25 open" || echo"TCP port 25 close" |
你可以 使用循環來查找開着的端口:
1 |
echo "Scanning TCP ports..." |
4 |
(echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open" |
輸出:
Scanning TCP ports...
22 open
53 open
80 open
139 open
445 open
631 open
下面的這個例子讓你的腳本扮演HTTP客戶端:
02 |
exec 3<> /dev/tcp/${1:-www.cyberciti.biz}/80 |
04 |
printf "GET / HTTP/1.0\r\n" >&3 |
05 |
printf "Accept: text/html, text/plain\r\n" >&3 |
06 |
printf "Accept-Language: en\r\n" >&3 |
07 |
printf "User-Agent: nixCraft_BashScript v.%s\r\n" "${BASH_VERSION}" >&3 |
12 |
# do something on $LINE |
13 |
# or send $LINE to grep or awk for grabbing data |
14 |
# or simply display back data with echo command |
關於GUITools和Cronjob
如果你使用cronjob來調用你的腳本的話,你要通過“ export DISPLAY=[user's machine]:0”命令來設置本地的 display/input 服務。例如調用 /home/vivek/scripts/monitor.stock.sh腳本,它使用了 zenity 工具:
1 |
@hourly DISPLAY=:0.0 /home/vivek/scripts/monitor.stock.sh |
所有的命令你都可以通過“man”來查詢詳細的使用方式。