Linux-shell get weather
Using the shell in Linux to get the weather is difficult. It turns out that it is really simple and the single-digit code will be done.
1. obtain the corresponding city weather
All weather information is obtained from China weather network. Each city corresponds to an id, for example, Beijing is 101010100. Because I am in Yinchuan, the id of Yinchuan is 101170101 in this example ), you can obtain real-time weather in the city or all-day weather by using the id, and seven days of weather.
1.1shell script
The shell script code is as follows:
- #!/bin/sh
- weatherDateRoot=http://www.weather.com.cn/data/sk/101170101.html
- weatherweatherDataFile=weather.html
- wget $weatherDateRoot -O $weatherDataFile > /dev/null 2>&1
- sed 's/.*temp":"\([0-9]\{1,2\}\).*/\1/g' $weatherDataFile
This script obtains the weather information and then matches the current temperature with the regular expression.
If you only use this script and do not perform secondary processing, it is too troublesome. After obtaining the weather information, it is displayed in the terminal command prompt, so you need to process it.
2. display the weather in the terminal Command Prompt
First, obtain the corresponding city weather information, such as Yinchuan's real-time weather information in:
Http://www.weather.com.cn/data/sk/101170101.html
First, you can search for the weather in the desired city on the China weather network. The URL will contain the city weather id, and you can get it by replacing the id in the above URL with your city id.
There are also all-day weather information:
Http://www.weather.com.cn/data/cityinfo/101170101.html
I don't know if the weather information provided by www.weather.cn for seven days is correct? If so, we can also use this method to obtain weather information for seven days.
2.1 obtain Weather Information
Corresponding shell script:
- #!/bin/sh
- allDataUrl=http://www.weather.com.cn/data/cityinfo/101170101.html
- allDataFile=/home/snowsolf/shell/weather/allDay.html
- dataUrl=http://www.weather.com.cn/data/sk/101170101.html
- dataFile=/home/snowsolf/shell/weather/weather.html
- wget $dataUrl -O $dataFile > /dev/null 2>&1
- wget $allDataUrl -O $allDataFile > /dev/null 2>&1
2.2 timed acquisition
Run the crontab command to set the scheduled task and run the crontab-e command. If you need to set the Default Editor for the first time, add the following at the end of the file:
- */30 * * * * /home/snowsolf/shell/weather/weather.sh >> /dev/null
This line of code is set to execute a weather script every 30 minutes. Other syntaxes of the specific crontab command can be google or baidu.
2.3 weather Extraction
- sed 's/.*temp":"\([0-9]\{1,2\}\).*/\1/g'
This command can obtain real-time weather from the obtained real-time weather file.
2.4 display in terminal Command Prompt
For more information, see http://www.cnblogs.com/snowsolf/p/3192224.html. This will make your command prompt more brilliant.
The last figure of my command prompt is as follows:
It's cool !!! It's really nice to be a cool programmer!