如何强化shell脚本

海外服务器 (499) 2015-11-28 09:47:30

很多人误以为shell脚本只能在命令行下使用。其实shell也可以调用一些GUI组件,例如菜单,警告框,进度条等等。你可以控制最终的输出,光标位置还有各种输出效果。下面我将介绍一些工具,帮助你创建强大的,互动的,用户友好的 Unix/Linux shell脚本。我在FreeBSD和Linux下测试过这些工具,不过其他UNIX系列的操作系统应该都支持的。

1. notify-send 命令

这个命令可以让你通过通知进程发送一个桌面通知给用户。这可以用来向用户发送提示,或者显示一些信息而不用打断用户工作。你需要安装如下软件包:

1.  $ sudo apt-get install libnotify-bin   

2.   

下面这个例子展示了如何从命令行向桌面发送一个简单的消息:

1.  notify-send "rsnapshot done :)"   

2.   

输出:

下面是一个复杂一点的例子:

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.      ... 

输出:

这里的参数解释如下:

  •  -t 5000:指定超时的时间,毫秒
  •  -u low:设置是否紧急
  •  -i gtk-dialog-info:通知图标,你可以指定图标 -i /path/to/your-icon.png

2. tput 命令

这个命令是用来设置终端特性的:

  •   移动光标
  •   获得终端信息
  •   设置前景和背景色
  •   设置粗体模式
  •   设置反模式等等

举例:

1.  01 #!/bin/bash   

2.   

3.  02      

4.   

5.  03 # clear the screen   

6.   

7.  04 tput clear   

8.   

9.  05      

10.  

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

12.  

13. 07 tput cup 3 15   

14.  

15. 08      

16.  

17. 09 # Set a foreground colour using ANSI escape   

18.  

19. 10 tput setaf 3   

20.  

21. 11 echo "XYX Corp LTD."   

22.  

23. 12 tput sgr0   

24.  

25. 13      

26.  

27. 14 tput cup 5 17   

28.  

29. 15 # Set reverse video mode   

30.  

31. 16 tput rev   

32.  

33. 17 echo "M A I N - M E N U"   

34.  

35. 18 tput sgr0   

36.  

37. 19      

38.  

39. 20 tput cup 7 15   

40.  

41. 21 echo "1. User Management"   

42.  

43. 22      

44.  

45. 23 tput cup 8 15   

46.  

47. 24 echo "2. Service Management"   

48.  

49. 25      

50.  

51. 26 tput cup 9 15   

52.  

53. 27 echo "3. Process Management"   

54.  

55. 28      

56.  

57. 29 tput cup 10 15   

58.  

59. 30 echo "4. Backup"   

60.  

61. 31      

62.  

63. 32 # Set bold mode   

64.  

65. 33 tput bold   

66.  

67. 34 tput cup 12 15   

68.  

69. 35 read -p "Enter your choice [1-4] " choice   

70.  

71. 36      

72.  

73. 37 tput clear   

74.  

75. 38 tput sgr0   

76.  

77. 39 tput rc   

78.  

输出:

3. setleds 命令

这个命令可以让你控制键盘灯,例如打开数字键盘灯:

1.  setleds -D +num   

2.   

关闭数字键盘灯:

1.  setleds -D -num   

2.   

  -caps: 清除大写灯

  •   +caps:打开大写灯
  •   -scroll:清除滚动锁
  •   +scroll:打开滚动锁

4. zenity 命令

这个命令可以显示GTK+的对话框,然后返回用户的输入。你可以用这个命令在脚本中显示信息,并要求用户输入信息。下面这段代码就是域名的whois查询:

1.  01 #!/bin/bash   

2.   

3.  02 # Get domain name   

4.   

5.  03 _zenity="/usr/bin/zenity"   

6.   

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

8.   

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

10.  

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

12.  

13. 07      

14.  

15. 08 if [ $? -eq 0 ]   

16.  

17. 09 then   

18.  

19. 10   # Display a progress dialog while searching whois database   

20.  

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

22.  

23. 12                       --title="whois" --progress \   

24.  

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

26.  

27. 14                                     --auto-kill --auto-close \   

28.  

29. 15                                     --percentage=10) >${_out}   

30.  

31. 16      

32.  

33. 17   # Display back output   

34.  

35. 18   ${_zenity} --width=800 --height=600  \   

36.  

37. 19          --title "Whois info for $domain" \   

38.  

39. 20          --text-info --filename="${_out}"   

40.  

41. 21 else   

42.  

43. 22   ${_zenity} --error \   

44.  

45. 23          --text="No input provided"   

46.  

47. 24 fi   

48.  

输出:

5. kdialog 命令

这个命令和zenity很想,只不过它是为KDE/QT应用准备的。使用方法如下:

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

2.   

输出

 

THE END