[LINUX] View today's weather forecast with a shell script

Introduction

As a practice of shell script, I made a script to check "today's weather" using Livedoor's weather forecast API Weather Hacks.

Also, I tried to specify the place name so that the weather forecast can be seen at the desired point.

script

Overview

How to use

weather.sh


#!/bin/bash
# http://weather.livedoor.com/forecast/webservice/json/v1?
​
#Extract the point names and IDs that match the entered place names from the point definition table (RSS) nationwide.
# http://weather.livedoor.com/forecast/rss/primary_area.xml
cityname_id=$(curl -s http://weather.livedoor.com/forecast/rss/primary_area.xml \
    | sed "s/>/>\n/g" \
    | grep city \
    | sed "s/<city title=\"//g" \
    | sed "s/\" source.*>//g" \
    | sed "s/\" id=\"/,/g" \
    | grep $1) \
​
#If the corresponding place name exists, get the weather forecast for that city.
#If the corresponding city does not exist, the process ends.
if [ -z "$cityname_id" ]; then
    echo 「$1 "weather forecast is not provided. Please choose from the following cities.
    echo ----------------------------------------
    echo $(curl -s http://weather.livedoor.com/forecast/rss/primary_area.xml \
        | sed "s/>/>\n/g" \
        | grep city \
        | sed "s/<city title=\"//g" \
        | sed "s/\" id.*>//g")
else
    # {Point name,ID}Extract only the ID from the character string of.
    city_id=$(echo $cityname_id | sed "s/.*,//g")
    city_name=$(echo $cityname_id | sed "s/,.*//g")
​
    echo $city_name today's weather:\
        $(curl -s http://weather.livedoor.com/forecast/webservice/json/v1?city=${city_id} \
            | jq '.forecasts[]' \
            | jq -r 'select(.dateLabel=="today").telop')
fi

Execution result

Execution result (when the weather forecast of the specified place name exists)


[root@akagi ~]# ./weather.sh tokyo
Today's weather in Tokyo: sunny

Execution result (when the weather forecast for the specified place name does not exist)


[root@akagi ~]# ./weather.sh penguins village
No weather forecast for "Penguins Village" is provided. Please choose from the following cities.
----------------------------------------
Wakkanai Asahikawa Rume Amisato Kitami Monbetsu Nemuro Kushiro Obihiro Muroran Urakawa Sapporo Iwamizawa Kuchiyasu Hakodate Esashi Aomori Mutsu Hachinohe Morioka Miyako
Funawatari Sendai Shiraishi Akita Yokote Yamagata Yonezawa Sakata Shinjo Fukushima Onahama Wakamatsu Mito Tsuchiura Utsunomiya Otahara Maebashi Minakami Saitama Bear
Tani Chichibu Chiba Chiba Tateyama Tokyo Oshima Hachijojima Father Island Yokohama Odawara Niigata Nagaoka Takada Aikawa Toyama Fushiki Kanazawa Wajima Fukui Tsuruga Kofu Kawaguchiko Nagano Matsumoto Iida Gifu Takayama Shizuoka Amiyo Mishima Owase Owase Owase Owase Owase Hamada Saigo Okayama Tsuyama Hiroshima Shohara Shimonoseki Yamaguchi Yanai Hagi Tokushima Hiwasa Takamatsu Matsuyama Araihama U
Wajima Kochi Cape Muroto Shimizu Fukuoka Hachiman Iizuka Kurume Saga Imari Nagasaki Sasebo Sagebo Fukue Kumamoto Aso Otohime Ushifuka Hitoshi Oita Nakatsu Hida Saeki Miyazaki Nobuoka Miyakonojo Takachiho Kagoshima Kagoshima Kunishima Nase

Summary

Recommended Posts

View today's weather forecast with a shell script
Tweet the weather forecast with a bot
Manage logrotate generations with a shell script.
Tweet the weather forecast with a bot Part 2
Try programming with a shell!
Let's try a shell script
Scraping weather forecast with python
[Linux] Copy data from Linux to Windows with a shell script
Process the files in the folder in order with a shell script
[Linux] Write a deployment tool using rsync with a shell script
Automate environment construction with Shell Script
Write a batch script with Python3.5 ~
Process the contents of the file in order with a shell script
[Ubuntu] How to execute a shell script
Create a star system with Blender 2.80 script
Creating a shell script to write a diary
Erase & generate serial number files with shell script
Launch a shell while a Python script is running
Let's do web scraping with Python (weather forecast)
Try creating a FizzBuzz problem with a shell program
Shell script basics # 2
A shell script that puts Webmin into Alpine Linux
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
Tips for opening a scene with a broken reference with a script