2018-04-09 11:37:40 2513瀏覽
有些誤解認(rèn)為shell腳本僅用于CLI環(huán)境。實際上在KDE或Gnome桌面下,你可以有效的使用各種工具編寫GUI或者網(wǎng)絡(luò)(socket)腳本。shell腳本可以使用一些GUI組件(菜單、警告框、進(jìn)度條等),你可以控制終端輸出、光標(biāo)位置以及各種輸出效果等等。利用下面的工具,你可以構(gòu)建強(qiáng)壯的、可交互的、對用戶友好的UNIX/Linuxbash腳本。
sudoapt-getinstalllibnotify-bin CentOS/RHEL用戶使用下面的yum命令: sudoyuminstalllibnotify FedoraLinux用戶使用下面的dnf命令: `$sudodnfinstalllibnotify` Inthisexample,sendsimpledesktopnotificationfromthecommandline,enter: ###發(fā)送一些通知### notify-send"rsnapshotdone:)"
示例輸出:
alert=18000 live=$(lynx--dumphttp://money.rediff.com/|grep'BSELIVE'|awk'{print$5}'|sed's/,//g;s/\.[0-9]*//g') [$notify_counter-eq0]&&[$live-ge$alert]&&{notify-send-t5000-ulow-i"BSESensextouched18k";notify_counter=1;}
示例輸出:
2、tput命令
tput命令用于設(shè)置終端特性。通過tput你可以設(shè)置:#!/bin/bash #clearthescreen tputclear #MovecursortoscreenlocationX,Y(topleftis0,0) tputcup315 #SetaforegroundcolourusingANSIescape tputsetaf3 echo"XYXCorpLTD." tputsgr0 tputcup517 #Setreversevideomode tputrev echo"MAIN-MENU" tputsgr0 tputcup715 echo"1.UserManagement" tputcup815 echo"2.ServiceManagement" tputcup915 echo"3.ProcessManagement" tputcup1015 echo"4.Backup" #Setboldmode tputbold tputcup1215 read-p"Enteryourchoice[1-4]"choice tputclear tputsgr0 tputrc
示例輸出:
setleds命令允許你設(shè)置鍵盤燈。下面是打開數(shù)字鍵燈的示例: setleds-D+num 關(guān)閉數(shù)字鍵燈,輸入: setleds-D-num -caps:關(guān)閉大小寫鎖定燈 +caps:打開大小寫鎖定燈 -scroll:關(guān)閉滾動鎖定燈 +scroll:打開滾動鎖定燈
#!/bin/bash #Getdomainname _zenity="/usr/bin/zenity" _out="/tmp/whois.output.$$" domain=$(${_zenity}--title"Enterdomain"\ --entry--text"Enterthedomainyouwouldliketoseewhoisinfo") if[$?-eq0] then #Displayaprogressdialogwhilesearchingwhoisdatabase whois$domain|tee>(${_zenity}--width=200--height=100\ --title="whois"--progress\ --pulsate--text="Searchingdomaininfo..."\ --auto-kill--auto-close\ --percentage=10)>${_out} #Displaybackoutput ${_zenity}--width=800--height=600\ --title"Whoisinfofor$domain"\ --text-info--filename="${_out}" else ${_zenity}--error\ --text="Noinputprovided" fi
示例輸出:
kdialog--dontagainmy:nofilemsg--msgbox"File:'~/.backup/config'notfound."
示例輸出:
#!/bin/bash dialog--title"Deletefile"\ --backtitle"LinuxShellTutorialExample"\ --yesno"Areyousureyouwanttopermanentlydelete\"/tmp/foo.txt\"?"760 #Getexitstatus #0meansuserhit[yes]button. #1meansuserhit[no]button. #255meansuserhit[Esc]key. response=$? case$responsein 0)echo"Filedeleted.";; 1)echo"Filenotdeleted.";; 255)echo"[ESC]keypressed.";; esac
logger"MySQLdatabasebackupfailed." tail-f/var/log/messages logger-tmysqld-pdaemon.error"DatabaseServerfailed" tail-f/var/log/syslog 示例輸出: Apr2000:11:45vivek-desktopkernel:[38600.515354]CPU0:Temperature/speednormal Apr2000:12:20vivek-desktopmysqld:DatabaseServerfailed
setterm-blank15-powersavepowerdown-powerdown60 下面的例子將xterm窗口中的文本以下劃線展示: setterm-underlineon; echo"AddYourImportantMessageHere" setterm-underlineoff 另一個有用的選項是打開或關(guān)閉光標(biāo)顯示: setterm-cursoroff 打開光標(biāo): setterm-cursoron
smbclient-MWinXPPro< Message1 Message2 ... .. EOF 或 echo"${Message}"|smbclient-Msalesguy2
#findoutifTCPport25openornot (echo>/dev/tcp/localhost/25)&>/dev/null&&echo"TCPport25open"||echo"TCPport25close"
echo"ScanningTCPports..." forpin{1..1023} do (echo>/dev/tcp/localhost/$p)>/dev/null2>&1&&echo"$popen" done 示例輸出: ScanningTCPports... 22open 53open 80open 139open 445open 631open 下面的示例中,你的bash腳本將像HTTP客戶端一樣工作: #!/bin/bash exec3<>/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_Bashv.%s\r\n""${BASH_VERSION}">&3 printf"\r\n">&3 whilereadLINE<&3 do #dosomethingon$LINE #orsend$LINEtogreporawkforgrabbingdata #orsimplydisplaybackdatawithechocommand echo$LINE done
最后體系如果你使用crontab來啟動你的腳本,你需要使用exportDISPLAY=[用戶機(jī)器]:0命令請求本地顯示或輸出服務(wù)。想要了解更多關(guān)于Linux發(fā)展前景趨勢,請關(guān)注扣丁學(xué)堂Linux培訓(xùn)官網(wǎng)、微信等平臺,扣丁學(xué)堂IT職業(yè)在線學(xué)習(xí)教育平臺為您提供權(quán)威的Linux視頻教程系統(tǒng),通過千鋒扣丁學(xué)堂金牌講師在線錄制的Linux視頻教程課程,讓你快速掌握Linux從入門到精通開發(fā)實戰(zhàn)技能。扣丁學(xué)堂Linux技術(shù)交流群:422345477。
【關(guān)注微信公眾號獲取更多的學(xué)習(xí)資料】