日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

linux shell脚本开发工具,技术|10个工具让你的 shell 脚本更强大

發(fā)布時(shí)間:2023/12/10 linux 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux shell脚本开发工具,技术|10个工具让你的 shell 脚本更强大 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

很多人誤以為shell腳本只能在命令行下使用。其實(shí)shell也可以調(diào)用一些GUI組件,例如菜單,警告框,進(jìn)度條等等。你可以控制最終的輸出,光標(biāo)位置還有各種輸出效果。下面我將介紹一些工具,幫助你創(chuàng)建強(qiáng)大的,互動(dòng)的,用戶友好的 Unix/Linux shell腳本。我在FreeBSD和Linux下測(cè)試過(guò)這些工具,不過(guò)其他UNIX系列的操作系統(tǒng)應(yīng)該都支持的。

1. notify-send 命令

這個(gè)命令可以讓你通過(guò)通知進(jìn)程發(fā)送一個(gè)桌面通知給用戶。這可以用來(lái)向用戶發(fā)送提示,或者顯示一些信息而不用打斷用戶工作。你需要安裝如下軟件包:

$ sudo apt-get install libnotify-bin

下面這個(gè)例子展示了如何從命令行向桌面發(fā)送一個(gè)簡(jiǎn)單的消息:

notify-send "rsnapshot done :)"

輸出:

?

下面是一個(gè)復(fù)雜一點(diǎn)的例子:

....

alert=18000

live=$(lynx --dump http://money.rediff.com/ | grep 'BSE LIVE' | awk '{ print $5}' | sed 's/,//g;s/\.[0-9]*//g')

[ $notify_counter -eq 0 ] && [ $live -ge $alert ] && { notify-send -t 5000 -u low -i "BSE Sensex touched 18k"; notify_counter=1; }

...

輸出:

?

這里的參數(shù)解釋如下:

-t 5000:指定超時(shí)的時(shí)間,毫秒

-u low:設(shè)置是否緊急

-i gtk-dialog-info:通知圖標(biāo),你可以指定圖標(biāo) -i /path/to/your-icon.png

2. tput 命令

這個(gè)命令是用來(lái)設(shè)置終端特性的:

移動(dòng)光標(biāo)

獲得終端信息

設(shè)置前景和背景色

設(shè)置粗體模式

設(shè)置反模式等等

舉例:

#!/bin/bash

# clear the screen

tput clear

# Move cursor to screen location X,Y (top left is 0,0)

tput cup 3 15

# Set a foreground colour using ANSI escape

tput setaf 3

echo "XYX Corp LTD."

tput sgr0

tput cup 5 17

# Set reverse video mode

tput rev

echo "M A I N - M E N U"

tput sgr0

tput cup 7 15

echo "1. User Management"

tput cup 8 15

echo "2. Service Management"

tput cup 9 15

echo "3. Process Management"

tput cup 10 15

echo "4. Backup"

# Set bold mode

tput bold

tput cup 12 15

read -p "Enter your choice [1-4] " choice

tput clear

tput sgr0

tput rc

輸出:

3. setleds 命令

這個(gè)命令可以讓你控制鍵盤燈,例如打開(kāi)數(shù)字鍵盤燈:

setleds -D +num

關(guān)閉數(shù)字鍵盤燈:

setleds -D -num

-caps: 清除大寫燈

+caps:打開(kāi)大寫燈

-scroll:清除滾動(dòng)鎖

+scroll:打開(kāi)滾動(dòng)鎖

4. zenity 命令

這個(gè)命令可以顯示GTK+的對(duì)話框,然后返回用戶的輸入。你可以用這個(gè)命令在腳本中顯示信息,并要求用戶輸入信息。下面這段代碼就是域名的whois查詢:

#!/bin/bash

# Get domain name

_zenity="/usr/bin/zenity"

_out="/tmp/whois.output.$$"

domain=$(${_zenity} --title "Enter domain" \

--entry --text "Enter the domain you would like to see whois info" )

if [ $? -eq 0 ]

then

# Display a progress dialog while searching whois database

whois $domain | tee >(${_zenity} --width=200 --height=100 \

--title="whois" --progress \

--pulsate --text="Searching domain info..." \

--auto-kill --auto-close \

--percentage=10) >${_out}

# Display back output

${_zenity} --width=800 --height=600 \

--title "Whois info for $domain" \

--text-info --filename="${_out}"

else

${_zenity} --error \

--text="No input provided"

fi

輸出:

5. kdialog 命令

這個(gè)命令和zenity很想,只不過(guò)它是為KDE/QT應(yīng)用準(zhǔn)備的。使用方法如下:

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

6. Dialog

這個(gè)命令可以在shell腳本中顯示文本組件。它使用了curses和ncurses類庫(kù)。示例代碼:

>#!/bin/bash

dialog --title "Delete file" \

--backtitle "Linux Shell Script Tutorial Example" \

--yesno "Are you sure you want to permanently delete \"/tmp/foo.txt\"?" 7 60

# Get exit status

# 0 means user hit [yes] button.

# 1 means user hit [no] button.

# 255 means user hit [Esc] key.

response=$?

case $response in

0) echo "File deleted.";;

1) echo "File not deleted.";;

255) echo "[ESC] key pressed.";;

esac

7. logger 命令

這個(gè)命令可以讓你寫入系統(tǒng)日志例如 /var/log/messages:

logger "MySQL database backup failed."

tail -f /var/log/messages

logger -t mysqld -p daemon.error "Database Server failed"

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 命令

這個(gè)命令可以設(shè)置中斷的屬性。下面的例子是強(qiáng)制屏幕全黑15分鐘,并且60分鐘后把顯示器設(shè)為待機(jī)狀態(tài):

setterm -blank 15 -powersave powerdown -powerdown 60

下面這段命令可以在中斷顯示加下劃線的文字:

setterm -underline on;

echo "Add Your Important Message Here"

setterm -underline off

或者你可以關(guān)閉光標(biāo):

setterm -cursor off

9. smbclient:

向 MS-Windows 系統(tǒng)發(fā)送消息

smbclient可以和 SMB/CIFS服務(wù)器通信。它可以向MS-Windows系統(tǒng)的指定用戶發(fā)送消息:

smbclient -M WinXPPro <

或者

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

10. Bash Socket 編程

你可以在bash中開(kāi)啟一個(gè)socket鏈接,并且傳輸數(shù)據(jù)。Bash有兩個(gè)特殊的設(shè)備文件:

/dev/tcp/host/port - 如果hostname,和port是合法的話,bash會(huì)嘗試開(kāi)啟一個(gè)TCP連接。

/dev/udp/host/port - 如果hostname和port是合法的話,bash會(huì)開(kāi)啟一個(gè)UDP連接。

你可以利用這個(gè)技術(shù)來(lái)測(cè)試一臺(tái)主機(jī)的端口是否是開(kāi)啟的,而不需要使用nmap或者port掃描器:

# find out if TCP port 25 open or not

(echo >/dev/tcp/localhost/25) &>/dev/null && echo "TCP port 25 open" || echo "TCP port 25 close"

echo "Scanning TCP ports..."

for p in {1..1023}

do

(echo >/dev/tcp/localhost/$p) >/dev/null 2>&1 && echo "$p open"

done

輸出:

Scanning TCP ports...

22 open

53 open

80 open

139 open

445 open

631 open

下面的這個(gè)例子讓你的腳本扮演HTTP客戶端:

#!/bin/bash

exec 3<> /dev/tcp/${1:-www.cyberciti.biz}/80

printf "GET / HTTP/1.0\r\n" >&3

printf "Accept: text/html, text/plain\r\n" >&3

printf "Accept-Language: en\r\n" >&3

printf "User-Agent: nixCraft_BashScript v.%s\r\n" "${BASH_VERSION}" >&3

printf "\r\n" >&3

while read LINE

do

# do something on $LINE

# or send $LINE to grep or awk for grabbing data

# or simply display back data with echo command

echo $LINE

done

關(guān)于GUITools和Cronjob

如果你使用cronjob來(lái)調(diào)用你的腳本的話,你要通過(guò)“?export DISPLAY=[user's machine]:0?”命令來(lái)設(shè)置本地的 display/input 服務(wù)。例如調(diào)用 /home/vivek/scripts/monitor.stock.sh腳本,它使用了 zenity 工具:

@hourly DISPLAY=:0.0 /home/vivek/scripts/monitor.stock.sh

所有的命令你都可以通過(guò)“man”來(lái)查詢?cè)敿?xì)的使用方式。

VIA?http://www.oschina.net/question/28_39527

總結(jié)

以上是生活随笔為你收集整理的linux shell脚本开发工具,技术|10个工具让你的 shell 脚本更强大的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。