linux shell實現自動獲取某地的天氣和空氣質量

因爲工作需要,實現了一個自動獲取某地天氣和空氣質量的shell程序。

#!/bin/bash

#時間:2015-6-21


#This module using to get the weather of Henan, from webpage:http://www.weather.com.cn/html/province/henan.shtml


echo "Program starting..."

#############################################################

#以下是變量定義,定義省(拼音和中文),市(拼音和中文)

province='河南'

province_PY='henan'

city='鄭州'

city_PY='zhengzhou'


#############################################################

#以下是傳出的變量,可以直接使用

#weather_state 

#天氣質量


#temperature1

#最高天氣溫度


#temperature2

#最低天氣溫度


#city_air

#天氣質量


#city_pm25

#pm2.5指數


#city_aqi

#aqi指數

#############################################################


        #Module 1, Wget the webpage from internet and saved as weather in current directory.


        echo "Getting the weather from webpage..."



        #The url where get the weather page. This page is from China Meteorologic Administration, Henan.


        url="http://www.weather.com.cn/html/province/"$province_PY".shtml"



        #date_doay is today's date, such as 2015-06-27


        date_today=$(date +'%Y-%m-%d')



        logFile=$date_today.log



        wget -O - $url > weather_province  




        echo "Got the weather file from internet."



        #Module 2, Analysize the weather file and getting the information


        echo "Getting the weather information..."




        #Geting the line where include the weather


        sed -n '/'${city}'天氣預報/p' weather_province > province.weather


        echo " Province weather file created."




        #As all the information at one line, replace <dl> as \n, making a city at a line


        sed -n '/<dl>/p' province.weather | sed 's/<dl>/\n/g' > city.weather


        echo " Citys weather file created."




        #Geting the city weather as format lines


        sed -n '/'${city}'/p' city.weather | sed 's/</\n</g' > one_city.weather


        echo " city Weather file created."




                #Analysize from the file to get the state information and temputer


                echo "     Anaysizing the weather state"


                weather_state=$(sed -n '/img alt/p' one_city.weather | awk -F '"' '{print $2}' | sed -n '1p')


                echo "The weather state is " $weather_state




                #Getting the tempture


                sed -n '/℃/p' one_city.weather | awk -F '>' '{print $2}'




                tempture1=$(sed -n '/℃/p' one_city.weather | awk -F '>' '{print $2}' | sed -n '1p')


                tempture2=$(sed -n '/℃/p' one_city.weather | awk -F '>' '{print $2}' | sed -n '2p')





                echo "The high tempture is " $tempture1


                echo "The low tempture is " $tempture2


#######################################################################

#以下的功能尋找天氣質量

#Getting the weather quality from http://www.pm25.com//XX.html



        echo " Getting the file from http://www.pm25.com//xx.html"


        air_url="www.pm25.com/"$city_PY.html


        wget -O - $air_url > city.air



        city_air=$(sed -n '/全部監測點/p' city.air | awk -F '"' '{print $12}')



        echo "The city air is " $city_air



        city_pm25=$(sed -n '/全部監測點/p' city.air | awk -F '"' '{print $10}')

​
        echo "The city pm 2.5 figure is: " $city_pm25


        city_aqi=$(sed -n '/全部監測點/p' city.air | awk -F '"' '{print $8}')


        echo "The city's air AQI index is:" $city_aqi

這裏記錄下,也讓自己記住,在使用sed帶參數的時候要用’${parameter_name}’才能被sed解析。

下面又對程序進行了改良,使用函數方式進行調用

#!/bin/bash

#時間:2015-6-21
#版本1.1,時間2015-7-6,更改腳本爲函數調用式
#author: Lion Lan

##########################################################
#GetWeatherAir函數說明:
#功能,傳入省市的信息,得到該城市的天氣和空氣參數
#1、參數說明
#   需要傳遞四個變量 省中文名稱 省拼音 市中文名稱 市拼音
#   調用示例 GetWeatherAir 河北 hebei 唐山 tangshan
#2、函數使用
#   默認函數中使用echo測試打印了取得的值(見註釋部分2),實際使用中可以註釋掉echo語句直接重定向輸出使用
##########################################################

function GetWeatherAir()
    {
        #This module using to get the weather of one province, such as from webpage:http://www.weather.com.cn/html/province/henan.shtml


        echo "Program starting..."

        #############################################################

        #以下是變量定義,定義省(拼音和中文),市(拼音和中文)

        province=$1

        province_PY=$2

        city=$3

        city_PY=$4


        ###########################註釋部分2##################################

        #以下是傳出的變量,可以直接使用

        #weather_state 

        #天氣質量


        #temperature1

        #最高天氣溫度


        #temperature2

        #最低天氣溫度


        #city_air

        #天氣質量


        #city_pm25

        #pm2.5指數


        #city_aqi

        #aqi指數

        #############################################################


            #Module 1, Wget the webpage from internet and saved as weather in current directory.


            echo "Getting the weather from webpage..."



            #The url where get the weather page. This page is from China Meteorologic Administration, Henan.


            url="http://www.weather.com.cn/html/province/"$province_PY".shtml"



            #date_doay is today's date, such as 2015-06-27


            date_today=$(date +'%Y-%m-%d')



            logFile=$date_today.log



            wget -O - $url > weather_province  




            echo "Got the weather file from internet."



            #Module 2, Analysize the weather file and getting the information


            echo "Getting the weather information..."




            #Geting the line where include the weather


            sed -n '/'${city}'天氣預報/p' weather_province > province.weather


            echo " Province weather file created."




            #As all the information at one line, replace <dl> as \n, making a city at a line


            sed -n '/<dl>/p' province.weather | sed 's/<dl>/\n/g' > city.weather


            echo " Citys weather file created."




            #Geting the city weather as format lines


            sed -n '/'${city}'/p' city.weather | sed 's/</\n</g' > one_city.weather


            echo " city Weather file created."




                #Analysize from the file to get the state information and temputer


                echo "     Anaysizing the weather state"


                weather_state=$(sed -n '/img alt/p' one_city.weather | awk -F '"' '{print $2}' | sed -n '1p')


                echo "The weather state is " $weather_state




                #Getting the tempture


                sed -n '/℃/p' one_city.weather | awk -F '>' '{print $2}'




                temperature1=$(sed -n '/℃/p' one_city.weather | awk -F '>' '{print $2}' | sed -n '1p')


                temperature2=$(sed -n '/℃/p' one_city.weather | awk -F '>' '{print $2}' | sed -n '2p')





                echo "The high temperature is " $temperature1


                echo "The low temperature is " $temperature2


        #######################################################################

        #以下的功能尋找天氣質量

        #Getting the weather quality from http://www.pm25.com//XX.html



            echo " Getting the file from http://www.pm25.com//xx.html"


            air_url="www.pm25.com/"$city_PY.html


            wget -O - $air_url > city.air



            city_air=$(sed -n '/全部監測點/p' city.air | awk -F '"' '{print $12}')



            echo "The city air is " $city_air



            city_pm25=$(sed -n '/全部監測點/p' city.air | awk -F '"' '{print $10}')


            echo "The city pm 2.5 figure is: " $city_pm25


            city_aqi=$(sed -n '/全部監測點/p' city.air | awk -F '"' '{print $8}')


            echo "The city's air AQI index is:" $city_aqi

    }

#####################測試函數###################################

GetWeatherAir 河南 henan 周口 zhoukou  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章