Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110573
  • 博文数量: 31
  • 博客积分: 691
  • 博客等级: 中士
  • 技术积分: 245
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-16 16:45
文章分类

全部博文(31)

文章存档

2012年(4)

2011年(27)

分类: LINUX

2011-06-18 22:06:09

闲着没事就写了一个让壁纸随机的脚本。现在还有很多bug。不过一有时间,我就会去fix的。谢谢大家的包涵。

闲话不多说。上代码。

  1. #!/bin/bash
  2. # randombg.sh
  3. # random background .

  4. cache=$HOME/.random_bg_cache/
  5. bgcommand="gconftool --type string -s /desktop/gnome/background/picture_filename"

  6. # check whether the cache directory is exist.
  7. if [[ -e "$cache" ]]
  8. then
  9. :
  10. else
  11. echo "The cache directory is not exist."
  12. if mkdir -p "$cache" >/dev/null 2>&1
  13. then
  14. echo "Creat the directory."
  15. else
  16. echo "Can't creat the directory."
  17. exit 3
  18. fi
  19. fi

  20. # get the picture name
  21. Get_wallimag(){
  22. local situ
  23. situ=$(ls -l $cache*.jpg 2>/dev/null| wc -l)
  24. if [ $situ -ne 0 ]
  25. then
  26. array=(`ls $cache*.jpg`)
  27. local sum=`ls -l $cache*.jpg 2>/dev/null | wc -l`
  28. local flag=$(($RANDOM % $sum))
  29. wallpg="${array[$flag]}"
  30. else
  31. echo "The cache directory is empty."
  32. echo "Please check it out."
  33. exit 3
  34. fi
  35. }

  36. Set_wallimag(){
  37. Get_wallimag
  38. if [ "$wallpg" = "" ]
  39. then
  40. echo "Get picture faild."
  41. exit 3
  42. fi
  43. $bgcommand $wallpg >/dev/null 2>&1
  44. if [ $? ]
  45. then
  46. :
  47. else
  48. echo "This job is faild."
  49. exit 2
  50. fi
  51. }

  52. # read arguments
  53. Read_arg(){
  54. while [[ $# -gt 0 ]]
  55. do
  56. parm="$1"
  57. shift

  58. case "$parm" in
  59. -h | --help)
  60. echo "-t time, --time time:
  61. Set the delay time(minute). e.g 3
  62. The time default is 3 minutes.
  63. -l , --list:
  64. List exist process.
  65. -r , --remove:
  66. Remove the current job.
  67. -v , --version:
  68. Show the current version.

  69. Notice:
  70. You must move some picture to $cache.
  71. "
  72. exit 0
  73. ;;
  74. -v | --version)
  75. echo "The current version:
  76. random 0.2"
  77. exit 0
  78. ;;
  79. -t | --time)
  80. TIME_CACHE="$1"
  81. if [ -z $TIME_CACHE ]
  82. then
  83. echo "Need a argument.
  84. randombg -t TIME"
  85. exit 0
  86. elif echo "$TIME_CACHE" | grep -q '[a-zA-Z]'
  87. then
  88. echo "Need a number.
  89. randombg -t NUMBER"
  90. exit 0
  91. fi
  92. TIME=$((TIME_CACHE*60))
  93. shift
  94. ;;
  95. -l | --list)
  96. local num
  97. num=$(ps -ef | grep randombg | grep -v grep|wc -l)
  98. if [ "$num" -gt 2 ]
  99. then
  100. echo "There is a process exist."
  101. ps -ef | grep "randombg" | grep -v 'grep' |head -n1| awk '{print $5" "$9" "$10" "$11}'
  102. exit 0
  103. else
  104. echo "There is no process exist."
  105. exit 1
  106. fi
  107. ;;
  108. -r | --remove)
  109. AIM=`ps -ef|grep randombg|grep -v grep| head -n1|awk '{print $2}'`
  110. kill $AIM
  111. exit 0
  112. ;;
  113. *)
  114. echo "Wrong input.
  115. randombg -h"
  116. exit 0
  117. ;;
  118. esac
  119. done
  120. }

  121. # loop forever
  122. Random_bg(){
  123. : ${TIME:=300}
  124. while :
  125. do
  126. Set_wallimag
  127. sleep $TIME
  128. done
  129. }

  130. # judge is there a randombg problem.
  131. Judge(){
  132. if [ `ps -ef | grep randombg | grep -v grep | wc -l` -gt 2 ]
  133. then
  134. ./randombg -l
  135. echo -n "There is randombg problem exist.Reset it now?(y/n)y "
  136. read ANSWER
  137. AIM=`ps -ef|grep randombg|grep -v grep| head -n1|awk '{print $2}'`
  138. : ${ANSWER:=y}
  139. case "$ANSWER" in
  140. y|Y)
  141. kill $AIM>/dev/null 2>&1
  142. Random_bg &
  143. exit 0
  144. ;;
  145. n|N)
  146. exit 0
  147. ;;
  148. *)
  149. exit 0
  150. ;;
  151. esac
  152. else
  153. Random_bg &
  154. exit 0
  155. fi
  156. }

  157. # main function
  158. Read_arg "$@"
  159. Judge
  160. exit 0

阅读(1506) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~