Linux-shell get weather

Source: Internet
Author: User

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:

 
 
  1. #!/bin/sh   
  2. weatherDateRoot=http://www.weather.com.cn/data/sk/101170101.html  
  3. weatherweatherDataFile=weather.html  
  4. wget $weatherDateRoot -O $weatherDataFile > /dev/null 2>&1   
  5. 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:

 
 
  1. #!/bin/sh  
  2. allDataUrl=http://www.weather.com.cn/data/cityinfo/101170101.html  
  3. allDataFile=/home/snowsolf/shell/weather/allDay.html  
  4. dataUrl=http://www.weather.com.cn/data/sk/101170101.html  
  5. dataFile=/home/snowsolf/shell/weather/weather.html  
  6. wget $dataUrl -O $dataFile > /dev/null 2>&1  
  7. 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:

 
 
  1. */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

 
 
  1. 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.