#!/bin/bash
# 都市名をコマンドライン引数から取得するか、ユーザーに尋ねる
city=$1
if [[ -z "$city" ]]; then
echo "都市名を入力してください:"
read city
if [[ -z "$city" ]]; then
echo "都市名が入力されませんでした。"
exit 1
fi
fi
# ansiweatherコマンドを実行して天気情報を表示
echo "ansiweatherの情報:"
if ! ansiweather -l "$city"; then
echo "ansiweatherから情報を取得できませんでした。"
fi
# curlコマンドを使用してwttr.inから天気情報を表示
echo "wttr.inの情報:"
if ! curl -s "wttr.in/${city}?lang=ja"; then
echo "wttr.inから情報を取得できませんでした。"
fi
#!/bin/bash
# 都市名を取得する関数
get_city() {
if [[ -z "$1" ]]; then
read -p "都市名を入力してください: " city
if [[ -z "$city" ]]; then
echo "エラー: 都市名が入力されていません。" >&2
return 1
fi
else
city="$1"
fi
# 入力値の検証
if [[ "$city" =~ ^[[:space:]]+$ ]]; then
echo "エラー: 都市名に空白のみが入力されています。" >&2
return 1
fi
return 0
}
# 天気情報を表示する関数
show_weather() {
local city="$1"
echo "--------------------"
echo "ansiweatherの情報 (${city}):"
if ! ansiweather -l "$city"; then
echo "警告: ansiweatherから情報を取得できませんでした。" >&2
fi
echo "--------------------"
echo "wttr.inの情報 (${city}):"
if ! curl -fs --connect-timeout 5 "wttr.in/${city}?lang=ja"; then
echo "警告: wttr.inから情報を取得できませんでした。" >&2
fi
}
# メイン処理
if [[ $# -eq 0 ]]; then
if ! get_city; then
exit 1
fi
show_weather "$city"
elif [[ $# -gt 0 ]]; then
for city in "$@"; do
if ! get_city "$city"; then
echo "$city の処理をスキップします。" >&2
continue
fi
show_weather "$city"
done
fi
exit 0