Get weather from Linux-shell and from linux-shell

Source: Internet
Author: User

Get weather from Linux-shell and from linux-shell

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.

Obtain the weather of the target City

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, so 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.

Shell script

The shell script code is as follows:

#!/bin/sh weatherDateRoot=http://www.weather.com.cn/data/sk/101170101.htmlweatherDataFile=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.

Display weather in 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.

Obtain Weather Information

Corresponding shell script:

#!/bin/shallDataUrl=http://www.weather.com.cn/data/cityinfo/101170101.htmlallDataFile=/home/snowsolf/shell/weather/allDay.htmldataUrl=http://www.weather.com.cn/data/sk/101170101.htmldataFile=/home/snowsolf/shell/weather/weather.htmlwget $dataUrl -O $dataFile > /dev/null 2>&1wget $allDataUrl -O $allDataFile > /dev/null 2>&1
Timed acquisition

Run the crontab command to set the scheduled task, run the crontab-e command (if you need to set the Default Editor for the first time), and 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.

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.

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!


Linux shell returns the date of a time period.

1. date. sh code
#! /Bin/sh

Beg_date = 'date-d "$1" + % s'
End_date = 'date-d "$2" + % s'

If [[-z $1] | [[-z $2]; then
Echo "Usage: $0 YYYYMMDD"
Exit 0;
Fi
If [[$ {beg_date} >$ {end_date}]; then
Echo "The end_date <beg_date; Please input the right date, example: $0 20140101 20140301"
Exit 0;
Fi
For (I =$ {beg_date}; I <=$ {end_date}; I = I + 86400 ))
Do
Date-d @ $ {I} + % Y % m % d
Done
2. instance./date. sh 20140101 20140201

How does LINUX shell obtain all files 24 hours before the current time? The files are named after the time.

24 hours, that is, the difference of one day, so only the number of days is changed.
Here is a stupid method:
Use date to get the current date and time, format it into your file name format, and use the following string truncation method to get the number of days:
$ {Varible: n1: n2}: truncate the string from n1 to n2
After the number of days minus 1, the new string is reorganized.
Use a loop to compare the name of the ls file with the string.
Run the code after the test.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.