Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Linux-shell to get weather conditions

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Linux-shell to obtain weather conditions". The explanation content in the article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian to study and learn "how to use Linux-shell to obtain weather conditions" together.

Getting Weather with Shell in Linux

1 Get the weather for the city

All weather information is obtained from China Weather Network. Each city will correspond to an id (for example, Beijing is 101010100, because I am in Yinchuan, so the id of Yinchuan is used in the example: 101170101). Through the id, you can obtain the real-time weather or all-day weather of the corresponding city, and you can also obtain the weather for seven days.

1.1 Shell scripts

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 captures weather information and then matches it to the current temperature through regularization.

If you only use this script and don't do secondary processing, it's too much trouble. After I get the weather information, it is displayed in the terminal command prompt, so I need to do more processing.

2 Display weather in terminal command prompt

First, the corresponding city weather is obtained, such as the real-time weather information corresponding to Yinchuan.

http://www.weather.com.cn/data/sk/101170101.html

You first search for the weather of the city you want in China Weather Network. The URL will contain the city weather id. Replace the id in the URL above with the id of your city to get it.

There is also all-day weather information:

http://www.weather.com.cn/data/cityinfo/101170101.html

Do not know if China Weather Network provides seven days of weather information? If there is, then we can also get seven days of weather information through this method.

2.1 acquire 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 timing acquisition

Set up timed tasks via crontab command, execute crontab -e command (if *** times you need to set default editor), then add at the end of file:

*/30 * * * * /home/snowsolf/shell/weather/weather.sh >> /dev/null

This line of code sets the script to execute once every 30 minutes to get the weather. Other syntax for the crontab command can be google or baidu.

2.3 Extract Weather

sed 's/.* temp":"\([0-9]\{1,2\}\).*/\ 1/g'

This command gets real-time weather from the retrieved real-time weather file.

2.4 terminal command prompt

You can refer to http://www.cnblogs.com/snowsolf/p/3192224.html. Here you can make your command prompt more colorful.

*** Previous picture of my command prompt:

Thank you for reading, the above is "how to use Linux-shell to obtain weather conditions" content, after the study of this article, I believe we have a deeper understanding of how to use Linux-shell to obtain weather conditions, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report