Last active
August 29, 2015 13:57
-
-
Save komasaru/9614307 to your computer and use it in GitHub Desktop.
Bash script to search NOAA weather stations by a ICAO code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 引数(ICAO コード)不正なら終了 | |
if [[ ! "$1" =~ [0-9A-Z]{4} ]]; then | |
echo "Wrong argument! - $1" | |
exit | |
fi | |
# 定数定義 | |
URL="http://www.aviationweather.gov/static/adds/metars/stations.txt" | |
FILE="stations.txt" | |
HEADER="CD STATION ICAO IATA SYNOP LAT LONG ELEV M N V U A C" | |
FLAG=0 | |
# satations.txt 取得 | |
wget -q -N ${URL} | |
# IFS は文字列リストのフィールド区切り文字で、デフォルトは半角スペース。 | |
# 行をそのまま取り込むために IFS を改行に変更 | |
OLDIFS="$IFS" | |
IFS="\n" | |
# 1行ずつ読み込み、引数と一致する ICAO コードの行を表示 | |
while read LINE | |
do | |
if [ "${LINE:20:4}" = $1 ]; then | |
echo $HEADER | |
echo $LINE | |
FLAG=1 | |
break | |
fi | |
done < "$FILE" | |
# 一致する ICAO コードが存在しなければ、メッセージ出力 | |
if [ $FLAG = 0 ]; then | |
echo "Not found! - $1" | |
fi | |
# IFS を元に戻す | |
IFS="$OLDIFS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment