现在的位置: 首页 > 综合 > 正文

一个shell脚本显示当地天气信息

2017年10月28日 ⁄ 综合 ⁄ 共 3787字 ⁄ 字号 评论关闭
一个shell脚本,功能是下载当地天气信息,并生成含对应信息的桌面背景图片,把它作为墙纸。
  1. #!/bin/bash
  2. #Copyright (c) 2009 xiooli (xioooli[at]yahoo.com.cn, http://joolix.com)
  3. #Name wallther
  4. #License: GPLv3
  5. #Version 20090511
  6. #此脚本需要安装 w3m 和 imagemagick
  7. #城市代码,留空可自动检测(自动检测不一定精确)
  8. #城市代码可在 http://weather.265.com 上查询,是个5位的数字
  9. #受bones7456和wenbob的天气脚本启发。
  10. #因为原有的查询地址无效,我就做了修改,使用了www.weather.com.cn。因为天气信息显示情况不同,所以我花了些时间修改sed和awk的语句。
  11. #使用的天气图标仍为原有的,但是背景图片我替换成了我更喜欢的火狐背景
  12. #alickee alick9188#163.com
  13. #许可仍为GPLv3
  14. #10/31/2009
  15. #这儿是城市代码,须自己改,未赋值会自动查询。暂时看来没什么用了。
  16. #由于更换了查询网站,这个代号就用不上了
  17. Wid=54511
  18. #地点拼音
  19. place=beijing
  20. #地点名称,后跟空格(用于匹配)
  21. place_cn="北京   "
  22. #天气图标的位置
  23. Icondir="/home/alickee/Program/shell_script/icons"
  24. #欲用作背景的图片
  25. BackPic="$Icondir/background.jpg"
  26. #最终输出的图片位置
  27. OutPic="/dev/shm/wallpapertmp.png"
  28. #壁纸路径(此为 OutPic 的副本)
  29. Wallpaper="/dev/shm/wallpaper.png"
  30. #是否在图片上绘制文字天气信息,yes/no
  31. DrawText="yes"
  32. #文字的字体,若不是中文字体则中文可能无法正常显示
  33. Font="/usr/share/fonts/zh_CN/TrueType/zysong.ttf"
  34. #文字的大小
  35. FontSize=24
  36. #文字的颜色
  37. TxtColor="white"
  38. #文字信息绘制的位置
  39. TxtPosX=700
  40. TxtPosY=350
  41. #隔多大距离绘制下一行(此距离包括本行的宽度)
  42. TxtYIncr=35
  43. #天气图标绘制的位置
  44. PicGeometry="+650+75"
  45. #壁纸更换的时间间隔(默认 30 分钟)
  46. ChangeTime="30m"
  47. WeatherCN=("晴" "多云" "阴" "雨" "雷阵雨" "雾" "雪" "雨夹雪")
  48. WeatherEN=("sun" "suncloud" "cloud" "rain" "storm" "fog" "snow" "snowrain")
  49. GET_WEATHER() {
  50. echo "获取天气中"
  51. #将获取天气信息的地址修改了
  52. WeatherTxt="`w3m -dump "http://www.weather.com.cn/${place}/index.shtml" \
  53. | grep "${place_cn}" | head -n 1 `"
  54. }
  55. GEN_DRAW_TEXT() {
  56. [ -z "${WeatherTxt}" ] && GET_WEATHER
  57. if [ -z "${WeatherTxt}" ]; then
  58. echo '未能获取天气 :( '
  59. else
  60. echo "${WeatherTxt}" | sed 's/[ ][ ]*/ /g' | awk '{print $1 " " $2 " " $3 "℃" "\n" $4 "风" $5 "\n降水" $6 " 湿度" $7}'
  61. fi \
  62. |sed "s/^.*$/-draw \\\'text POSITION \\\"&\\\"\\\'/" \
  63. |while read line; do
  64. echo "$line"|sed "s/POSITION/$TxtPosX,$TxtPosY/"
  65. ((TxtPosY+=$TxtYIncr))
  66. done|tr "\n" " "
  67. }
  68. GEN_WEATHER_ICON() {
  69. local tmp weathercn index
  70. [ -z "${WeatherTxt}" ] && GET_WEATHER
  71. [ -z "${WeatherTxt}" ] || tmp="`echo "${WeatherTxt}" | awk '{print $2}'`"
  72. j=0; k=0
  73. for i in ${WeatherCN[@]}; do
  74. [ "${tmp//$i}" != "$tmp" ] && weathercn[$j]="$i" && index[$j]="$k" && ((j++))
  75. ((k++))
  76. done
  77. # ${#parameter}
  78.        #     The length in characters of the value of   parameter   is   substi-
  79.        #     tuted. If   parameter   is   * or @, the value substituted is the
  80.        #     number of positional parameters.   If parameter is an array   name
  81.        #     subscripted   by   *   or @, the value substituted is the number of
  82.        #     elements in the array.
  83. #调试时输出weathercn数组元素的个数
  84. #echo "weathercn num=" "${#weathercn[@]}"
  85. [ "${#weathercn[@]}" -eq 0 ] && Weather[0]="unknown"
  86. [ "${#weathercn[@]}" -eq 1 ] && Weather[0]="${WeatherEN[${index[0]}]}"
  87. [ "${#weathercn[@]}" -eq 2 ] && \
  88. if [ "`echo $tmp|grep "${weathercn[0]}转"`" ];then
  89. Weather[0]="${WeatherEN[${index[0]}]}"
  90. Weather[1]="${WeatherEN[${index[1]}]}"
  91. else
  92. Weather[0]="${WeatherEN[${index[1]}]}"
  93. Weather[1]="${WeatherEN[${index[0]}]}"
  94. fi
  95. [ "${#weathercn[@]}" -eq 3 ] && \
  96. {
  97. Weather[0]="${WeatherEN[${index[0]}]}"
  98. Weather[1]="${WeatherEN[${index[2]}]}"
  99. }
  100. if [ "${#Weather[@]}" -ge 2 ]; then
  101.    convert +append "$Icondir/${Weather[0]}.png" "$Icondir/${Weather[1]}.png" /dev/shm/weathericon.png
  102.    #因为生成的图片宽度变为原来的两倍,故再更改下大小
  103.    convert -resize   50%x100% /dev/shm/weathericon.png /dev/shm/weathericon.tmp.png
  104.    mv /dev/shm/weathericon.tmp.png /dev/shm/weathericon.png
  105. else
  106.    ln -sf "$Icondir/${Weather[0]}.png" /dev/shm/weathericon.png
  107. fi
  108. }
  109. GEN_WALLPAPWE() {
  110. GEN_WEATHER_ICON
  111. [ -f $BackPic ] || BackPic="$Icondir/background.jpg"
  112. if [ "$DrawText" = "yes" ]; then
  113. draw="convert -font \"$Font\" -fill $TxtColor -pointsize $FontSize `GEN_DRAW_TEXT` \"$BackPic\" \"/dev/shm/backpictmp.png\""
  114. eval "$draw"
  115. composite -geometry "$PicGeometry" /dev/shm/weathericon.png /dev/shm/backpictmp.png "$OutPic"
  116. else
  117. composite -geometry "$PicGeometry" /dev/shm/weathericon.png "$BackPic" "$OutPic"
  118. fi
  119. }
  120. while :; do
  121. GET_WEATHER
  122. echo "站名    天气    气温       风向       风速    降水     湿度"
  123. echo "$WeatherTxt"
  124. GEN_WALLPAPWE
  125. #下面一行显示通知区域小图标
  126. #zenity --notification --window-icon=/usr/share/icons/gnome/48x48/filesystems/gnome-fs-bookmark.png --timeout=30 --text="${WeatherTxt}"
  127. if [ -f "$OutPic" ];then
  128. mv "$OutPic" "$Wallpaper"
  129. #更换桌面背景
  130. gconftool-2 -s /desktop/gnome/background/picture_filename --type=string "$Wallpaper"
  131. fi
  132. sleep "$ChangeTime"
  133. done

抱歉!评论已关闭.