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.
sed
command.jq
command is displayed.weather.sh
, specify the desired place name as an argument.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 (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
Recommended Posts