2018-04-09 11:37:40 1348浏览
有些误解认为shell脚本仅用于CLI环境。实际上在KDE或Gnome桌面下,你可以有效的使用各种工具编写GUI或者网络(socket)脚本。shell脚本可以使用一些GUI组件(菜单、警告框、进度条等),你可以控制终端输出、光标位置以及各种输出效果等等。利用下面的工具,你可以构建强壮的、可交互的、对用户友好的UNIX/Linuxbash脚本。
sudoapt-getinstalllibnotify-bin CentOS/RHEL用户使用下面的yum命令: sudoyuminstalllibnotify FedoraLinux用户使用下面的dnf命令: `$sudodnfinstalllibnotify` Inthisexample,sendsimpledesktopnotificationfromthecommandline,enter: ###发送一些通知### 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命令用于设置终端特性。通过tput你可以设置:#!/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命令允许你设置键盘灯。下面是打开数字键灯的示例: setleds-D+num 关闭数字键灯,输入: setleds-D-num -caps:关闭大小写锁定灯 +caps:打开大小写锁定灯 -scroll:关闭滚动锁定灯 +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 另一个有用的选项是打开或关闭光标显示: setterm-cursoroff 打开光标: 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=[用户机器]:0命令请求本地显示或输出服务。想要了解更多关于Linux发展前景趋势,请关注扣丁学堂Linux培训官网、微信等平台,扣丁学堂IT职业在线学习教育平台为您提供权威的Linux视频教程系统,通过千锋扣丁学堂金牌讲师在线录制的Linux视频教程课程,让你快速掌握Linux从入门到精通开发实战技能。扣丁学堂Linux技术交流群:422345477。
【关注微信公众号获取更多的学习资料】