乙個shell指令碼顯示當地天氣資訊

2021-06-19 10:06:20 字數 3627 閱讀 4557

#!/bin/bash

#name wallther

#license: gplv3

#version 20090511

#此指令碼需要安裝 w3m 和 imagemagick

#城市**,留空可自動檢測(自動檢測不一定精確)

#城市**可在 上查詢,是個5位的數字

#受bones7456和wenbob的天氣指令碼啟發。

#因為原有的查詢位址無效,我就做了修改,使用了www.weather.com.cn。因為天氣資訊顯示情況不同,所以我花了些時間修改sed和awk的語句。

#使用的天氣圖示仍為原有的,但是背景我替換成了我更喜歡的火狐背景

#alickee alick9188#163.com

#許可仍為gplv3

#10/31/2009

#這兒是城市**,須自己改,未賦值會自動查詢。暫時看來沒什麼用了。

#由於更換了查詢**,這個代號就用不上了

wid=54511

#地點拼音

place=beijing

#地點名稱,後跟空格(用於匹配)

place_cn="北京   "

#天氣圖標的位置

icondir="/home/alickee/program/shell_script/icons"

#欲用作背景的

#最終輸出的位置

outpic="/dev/shm/wall*****tmp.png"

#桌布路徑(此為 outpic 的副本)

wall*****="/dev/shm/wall*****.png"

#是否在上繪製文字天氣資訊,yes/no

drawtext="yes"

#文字的字型,若不是中文字型則中文可能無法正常顯示

font="/usr/share/fonts/zh_cn/truetype/zysong.ttf"

#文字的大小

fontsize=24

#文字的顏色

txtcolor="white"

#文字資訊繪製的位置

txtposx=700

txtposy=350

#隔多大距離繪製下一行(此距離包括本行的寬度)

txtyincr=35

#天氣圖示繪製的位置

picgeometry="+650+75"

#桌布更換的時間間隔(預設 30 分鐘)

changetime="30m"

weathercn=("晴" "多雲" "陰" "雨" "雷陣雨" "霧" "雪" "雨夾雪")

weatheren=("sun" "suncloud" "cloud" "rain" "storm" "fog" "snow" "snowrain")

get_weather() /index.shtml" \

| grep "$" | head -n 1 `"

}gen_draw_text() " ] && get_weather

if [ -z "$" ]; then

echo '未能獲取天氣 :( '

else

echo "$" | sed 's/[ ][ ]*/ /g' | awk ''

fi \

|sed "s/^.*$/-draw \\\'text position \\\"&\\\"\\\'/" \

|while read line; do

echo "$line"|sed "s/position/$txtposx,$txtposy/"

((txtposy+=$txtyincr))

done|tr "\n" " "

}gen_weather_icon() " ] && get_weather

[ -z "$" ] || tmp="`echo "$" | awk ''`"

j=0; k=0

for i in $; do

[ "$" != "$tmp" ] && weathercn[$j]="$i" && index[$j]="$k" && ((j++))

((k++))

done

# $#     the length in characters of the value of   parameter   is   substi-

#     tuted. if   parameter   is   * or @, the value substituted is the

#     number of positional parameters.   if parameter is an array   name

#     subscripted   by   *   or @, the value substituted is the number of

#     elements in the array.

#除錯時輸出weathercn陣列元素的個數

#echo "weathercn num=" "$"

[ "$" -eq 0 ] && weather[0]="unknown"

[ "$" -eq 1 ] && weather[0]="$]}"

[ "$" -eq 2 ] && \

if [ "`echo $tmp|grep "$轉"`" ];then

weather[0]="$]}"

weather[1]="$]}"

else

weather[0]="$]}"

weather[1]="$]}"

fi[ "$" -eq 3 ] && \

]}"weather[1]="$]}"

}if [ "$" -ge 2 ]; then

#因為生成的寬度變為原來的兩倍,故再更改下大小

convert -resize   50%x100% /dev/shm/weathericon.png /dev/shm/weathericon.tmp.png

mv /dev/shm/weathericon.tmp.png /dev/shm/weathericon.png

else

ln -sf "$icondir/$.png" /dev/shm/weathericon.pngfi}

gen_wallpapwe()

while :; do

get_weather

echo "站名    天氣    氣溫       風向       風速    降水     濕度"

echo "$weathertxt"

gen_wallpapwe

#下面一行顯示通知區域小圖示

#zenity --notification --window-icon=/usr/share/icons/gnome/48x48/filesystems/gnome-fs-bookmark.png --timeout=30 --text="$"

if [ -f "$outpic" ];then

mv "$outpic" "$wall*****"

#更換桌面背景

gconftool-2 -s /desktop/gnome/background/picture_filename --type=string "$wall*****"

fisleep "$changetime"

done

乙個shell指令碼

之前學過shell指令碼,但是工作中一直沒有用到,今天由於要搞乙個定時任務,為了防止重複啟動php指令碼,就需要寫乙個shell,下面是指令碼內容 bin bash 宣告要使用哪個shell 下面是備註檔案 這可是乙個好習慣奧 date 2012 7 18 version 1.0 設定相關變數 fi...

shell指令碼練習 每天乙個shell指令碼 7

我的部落格都是依照自己淺薄的現有知識去寫的,好多時候寫完了部落格,去查詢答案,會發現很多更剪短更效率更好的答案。寫部落格是激勵我自己能堅持下去,讓我的頭腦不去懈怠,望共勉。題目 write a script five dirs.sh that does these tasks make a dire...

Shell指令碼學習5 第乙個Shell指令碼

開啟文字編輯器,新建乙個檔案,擴充套件名為sh sh代表shell 副檔名並不影響指令碼執行,見名知意就好,如果你用php寫shell 指令碼,副檔名就用php好了。輸入一些 bin bash echo hello world 是乙個約定的標記,它告訴系統這個指令碼需要什麼直譯器來執行,即使用哪一種...