自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

10個(gè)增加UNIX/Linux Shell腳本趣味的工具

系統(tǒng) Linux
有些誤解認(rèn)為 shell 腳本僅用于CLI環(huán)境。實(shí)際上在 KDE 或 Gnome 桌面下,你可以有效的使用各種工具編寫 GUI 或者網(wǎng)絡(luò)腳本。shell 腳本可以使用一些GUI 件,你可以控制終端輸出、光標(biāo)位置以及各種輸出效果等等。利用下面的工具,你可以構(gòu)建強(qiáng)壯的、可交互的、對(duì)用戶友好的 UNIX/Linux bash 腳本。

[[224865]]

有些誤解認(rèn)為 shell 腳本僅用于 CLI 環(huán)境。實(shí)際上在 KDE 或 Gnome 桌面下,你可以有效的使用各種工具編寫 GUI 或者網(wǎng)絡(luò)(socket)腳本。shell 腳本可以使用一些 GUI 組件(菜單、警告框、進(jìn)度條等),你可以控制終端輸出、光標(biāo)位置以及各種輸出效果等等。利用下面的工具,你可以構(gòu)建強(qiáng)壯的、可交互的、對(duì)用戶友好的 UNIX/Linux bash 腳本。

制作 GUI 應(yīng)用不是一項(xiàng)困難的任務(wù),但需要時(shí)間和耐心。幸運(yùn)的是,UNIX 和 Linux 都帶有大量編寫漂亮 GUI 腳本的工具。以下工具是基于 FreeBSD 和 Linux 操作系統(tǒng)做的測(cè)試,而且也適用于其他類 UNIX 操作系統(tǒng)。 

1、notify-send 命令

notify-send 命令允許你借助通知守護(hù)進(jìn)程發(fā)送桌面通知給用戶。這種避免打擾用戶的方式,對(duì)于通知桌面用戶一個(gè)事件或顯示一些信息是有用的。在 Debian 或 Ubuntu 上,你需要使用 apt 命令apt-get 命令 安裝的包:

  1. sudo apt-get install libnotify-bin

CentOS/RHEL 用戶使用下面的 yum 命令

  1. sudo yum install libnotify

Fedora Linux 用戶使用下面的 dnf 命令:

  1. `$ sudo dnf install libnotify`
  2. In this example, send simple desktop notification from the command line, enter:
  3. ### 發(fā)送一些通知 ###
  4. notify-send "rsnapshot done :)"

示例輸出:

Fig:01: notify-send in action

notify-send: Shell Script Get Or Send Desktop Notifications

下面是另一個(gè)附加選項(xiàng)的代碼:

  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 -eq 0 ] && [ $live -ge $alert ] && { notify-send -t 5000 -u low -i "BSE Sensex touched 18k"; notify_counter=1; }
  5. ...

示例輸出:

Fig.02: notify-send with timeouts and other options

Linux / UNIX: Display Notifications From Your Shell Scripts With notify-send

這里:

  • -t 5000:指定超時(shí)時(shí)間(毫秒) (5000 毫秒 = 5 秒)
  • -u low: 設(shè)置緊急等級(jí) (如:低、普通、緊急)
  • -i gtk-dialog-info: 設(shè)置要顯示的圖標(biāo)名稱或者指定的圖標(biāo)(你可以設(shè)置路徑為:-i /path/to/your-icon.png

關(guān)于更多使用 notify-send 功能的信息,請(qǐng)參考 man 手冊(cè)。在命令行下輸入 man notify-send 即可看見:

  1. man notify-send 

2、tput 命令

tput 命令用于設(shè)置終端特性。通過 tput 你可以設(shè)置:

  • 在屏幕上移動(dòng)光標(biāo)。
  • 獲取終端信息。
  • 設(shè)置顏色(背景和前景)。
  • 設(shè)置加粗模式。
  • 設(shè)置反轉(zhuǎn)模式等等。

下面有一段示例代碼:

  1. #!/bin/bash
  2.  
  3. # clear the screen
  4. tput clear
  5.  
  6. # Move cursor to screen location X,Y (top left is 0,0)
  7. tput cup 3 15
  8.  
  9. # Set a foreground colour using ANSI escape
  10. tput setaf 3
  11. echo "XYX Corp LTD."
  12. tput sgr0
  13.  
  14. tput cup 5 17
  15. # Set reverse video mode
  16. tput rev
  17. echo "M A I N - M E N U"
  18. tput sgr0
  19.  
  20. tput cup 7 15
  21. echo "1. User Management"
  22.  
  23. tput cup 8 15
  24. echo "2. Service Management"
  25.  
  26. tput cup 9 15
  27. echo "3. Process Management"
  28.  
  29. tput cup 10 15
  30. echo "4. Backup"
  31.  
  32. # Set bold mode
  33. tput bold
  34. tput cup 12 15
  35. read -p "Enter your choice [1-4] " choice
  36.  
  37. tput clear
  38. tput sgr0
  39. tput rc

示例輸出:

Fig.03: tput in action

Linux / UNIX Script Colours and Cursor Movement With tput

關(guān)于 tput 命令的詳細(xì)信息,參見手冊(cè):

  1. man 5 terminfo
  2. man tput 

3、setleds 命令

setleds 命令允許你設(shè)置鍵盤燈。下面是打開數(shù)字鍵燈的示例:

  1. setleds -D +num

關(guān)閉數(shù)字鍵燈,輸入:

  1. setleds -D -num
  • -caps:關(guān)閉大小寫鎖定燈
  • +caps:打開大小寫鎖定燈
  • -scroll:關(guān)閉滾動(dòng)鎖定燈
  • +scroll:打開滾動(dòng)鎖定燈

查看 setleds 手冊(cè)可看見更多信息和選項(xiàng) man setleds。 

4、zenity 命令

zenity 命令顯示 GTK+ 對(duì)話框,并且返回用戶輸入。它允許你使用各種 Shell 腳本向用戶展示或請(qǐng)求信息。下面是一個(gè) whois 指定域名目錄服務(wù)的 GUI 客戶端示例。

  1. #!/bin/bash
  2. # Get domain name
  3. _zenity="/usr/bin/zenity"
  4. _out="/tmp/whois.output.$$"
  5. domain=$(${_zenity} --title "Enter domain" \
  6. --entry --text "Enter the domain you would like to see whois info" )
  7.  
  8. if [ $? -eq 0 ]
  9. then
  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}
  16.  
  17. # Display back output
  18. ${_zenity} --width=800 --height=600 \
  19. --title "Whois info for $domain" \
  20. --text-info --filename="${_out}"
  21. else
  22. ${_zenity} --error \
  23. --text="No input provided"
  24. fi

示例輸出:

Fig.04: zenity in Action

zenity: Linux / UNIX display Dialogs Boxes From The Shell Scripts

參見手冊(cè)獲取更多 zenity 信息以及其他支持 GTK+ 的組件:

  1. zenity --help
  2. man zenity 

5、kdialog 命令

kdialog 命令與 zenity 類似,但它是為 KDE 桌面和 QT 應(yīng)用設(shè)計(jì)。你可以使用 kdialog 展示對(duì)話框。下面示例將在屏幕上顯示信息:

  1. kdialog --dontagain myscript:nofilemsg --msgbox "File: '~/.backup/config' not found."

示例輸出:

Fig.05: Suppressing the display of a dialog

Kdialog: Suppressing the display of a dialog

參見 《KDE 對(duì)話框 Shell 腳本編程》 教程獲取更多信息。 

6、Dialog

Dialog 是一個(gè)使用 Shell 腳本的應(yīng)用,顯示用戶界面組件的文本。它使用 curses 或者 ncurses 庫。下面是一個(gè)示例代碼:

  1. #!/bin/bash
  2. dialog --title "Delete file" \
  3. --backtitle "Linux Shell Script Tutorial Example" \
  4. --yesno "Are you sure you want to permanently delete \"/tmp/foo.txt\"?" 7 60
  5.  
  6. # Get exit status
  7. # 0 means user hit [yes] button.
  8. # 1 means user hit [no] button.
  9. # 255 means user hit [Esc] key.
  10. response=$?
  11. case $response in
  12. 0) echo "File deleted.";;
  13. 1) echo "File not deleted.";;
  14. 255) echo "[ESC] key pressed.";;
  15. esac

參見 dialog 手冊(cè)獲取詳細(xì)信息:man dialog。 

關(guān)于其他用戶界面工具的注意事項(xiàng)

UNIX、Linux 提供了大量其他工具來顯示和控制命令行中的應(yīng)用程序,shell 腳本可以使用一些 KDE、Gnome、X 組件集:

  • gmessage - 基于 GTK xmessage 的克隆
  • xmessage - 在窗口中顯示或詢問消息(基于 X 的 /bin/echo)
  • whiptail - 顯示來自 shell 腳本的對(duì)話框
  • python-dialog - 用于制作簡(jiǎn)單文本或控制臺(tái)模式用戶界面的 Python 模塊 

7、logger 命令

logger 命令將信息寫到系統(tǒng)日志文件,如:/var/log/messages。它為系統(tǒng)日志模塊 syslog 提供了一個(gè) shell 命令行接口:

  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

示例輸出:

  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

參見 《如何寫消息到 syslog 或 日志文件》 獲得更多信息。此外,你也可以查看 logger 手冊(cè)獲取詳細(xì)信息:man logger 

8、setterm 命令

setterm 命令可設(shè)置不同的終端屬性。下面的示例代碼會(huì)強(qiáng)制屏幕在 15 分鐘后變黑,監(jiān)視器則 60 分鐘后待機(jī)。

  1. setterm -blank 15 -powersave powerdown -powerdown 60

下面的例子將 xterm 窗口中的文本以下劃線展示:

  1. setterm -underline on;
  2. echo "Add Your Important Message Here"
  3. setterm -underline off

另一個(gè)有用的選項(xiàng)是打開或關(guān)閉光標(biāo)顯示:

  1. setterm -cursor off

打開光標(biāo):

  1. setterm -cursor on

參見 setterm 命令手冊(cè)獲取詳細(xì)信息:man setterm 

9、smbclient:給 MS-Windows 工作站發(fā)送消息

smbclient 命令可以與 SMB/CIFS 服務(wù)器通訊。它可以向 MS-Windows 系統(tǒng)上選定或全部用戶發(fā)送消息。

  1. smbclient -M WinXPPro <<eof
  2. Message 1
  3. Message 2
  4. ...
  5. ..
  6. EOF

  1. echo "${Message}" | smbclient -M salesguy2

參見 smbclient 手冊(cè)或者閱讀我們之前發(fā)布的文章:《給 Windows 工作站發(fā)送消息》:man smbclient 

10、Bash 套接字編程

在 bash 下,你可以打開一個(gè)套接字并通過它發(fā)送數(shù)據(jù)。你不必使用 curl 或者 lynx 命令抓取遠(yuǎn)程服務(wù)器的數(shù)據(jù)。bash 和兩個(gè)特殊的設(shè)備文件可用于打開網(wǎng)絡(luò)套接字。以下選自 bash 手冊(cè):

  1. /dev/tcp/host/port - 如果 host 是一個(gè)有效的主機(jī)名或者網(wǎng)絡(luò)地址,而且端口是一個(gè)整數(shù)或者服務(wù)名,bash 會(huì)嘗試打開一個(gè)相應(yīng)的 TCP 連接套接字。
  2. /dev/udp/host/port - 如果 host 是一個(gè)有效的主機(jī)名或者網(wǎng)絡(luò)地址,而且端口是一個(gè)整數(shù)或者服務(wù)名,bash 會(huì)嘗試打開一個(gè)相應(yīng)的 UDP 連接套接字。

你可以使用這項(xiàng)技術(shù)來確定本地或遠(yuǎn)程服務(wù)器端口是打開或者關(guān)閉狀態(tài),而無需使用 nmap 或者其它的端口掃描器。

  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"

下面的代碼片段,你可以利用 bash 循環(huán)找出已打開的端口

  1. echo "Scanning TCP ports..."
  2. for p in {1..1023}
  3. do
  4. (echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open"
  5. done

示例輸出:

  1. Scanning TCP ports...
  2. 22 open
  3. 53 open
  4. 80 open
  5. 139 open
  6. 445 open
  7. 631 open

下面的示例中,你的 bash 腳本將像 HTTP 客戶端一樣工作:

  1. #!/bin/bash
  2. exec 3<> /dev/tcp/${1:-www.cyberciti.biz}/80
  3.  
  4. printf "GET / HTTP/1.0\r\n" >&3
  5. printf "Accept: text/html, text/plain\r\n" >&3
  6. printf "Accept-Language: en\r\n" >&3
  7. printf "User-Agent: nixCraft_BashScript v.%s\r\n" "${BASH_VERSION}" >&3
  8. printf "\r\n" >&3
  9.  
  10. while read LINE <&3
  11. do
  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
  15. echo $LINE
  16. done

參見 bash 手冊(cè)獲取更多信息:man bash 

關(guān)于 GUI 工具和 cron 任務(wù)的注意事項(xiàng)

如果你 使用 crontab 來啟動(dòng)你的腳本,你需要使用 export DISPLAY=[用戶機(jī)器]:0 命令請(qǐng)求本地顯示或輸出服務(wù)。舉個(gè)例子,使用 zenity 工具調(diào)用 /home/vivek/scripts/monitor.stock.sh

  1. @hourly DISPLAY=:0.0 /home/vivek/scripts/monitor.stock.sh
責(zé)任編輯:龐桂玉 來源: Linux中國
相關(guān)推薦

2014-03-13 16:34:04

LinuxBashKSH shel

2010-03-23 16:35:31

shell 腳本編程

2022-08-30 08:52:04

shell腳本Linux

2018-02-01 17:32:30

LinuxUNIXBash Shell

2014-07-25 17:21:43

Linux Shell面試

2020-06-16 09:02:36

Linux Shell腳本

2014-05-28 09:26:57

Linux Shellshell腳本

2009-09-29 10:45:17

UnixLinuxshell

2009-10-23 13:44:03

linux Shell

2010-04-16 14:46:22

2011-05-11 09:29:43

LinuxUnix

2010-06-23 15:55:36

Linux Bash

2021-05-12 10:17:15

Shell工具Linux

2024-11-27 09:19:25

2016-10-31 08:35:20

LinuxUnix

2013-06-13 11:07:52

2019-08-09 13:50:08

shellLinux

2017-08-11 17:20:07

LinuxShell

2017-06-19 15:46:08

LinuxBash腳本技巧

2017-08-15 11:32:21

LinuxBash腳本技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)