Bashで既存の応答のエラーチェックを実行するには?

Bashで既存の応答のエラーチェックを実行するには?

city-data.comから一部のデータを削除しようとしています。私は都市と州を取得したいと思います。これが私ができることです。しかし、郵便番号がないとif/elseを実行できないようです。

baseURL="https://www.city-data.com/zips"
errorResponse=" <title>Page not found - City-Data.com</title>"

location=$( curl -s -dump "$baseURL/$1.html" | grep -i '<title>' | cut -d\( -f2 | cut -d\) -f1 )

if $location = $errorResponse;
then
  echo "That zipcode does not exist"
  exit 0
else
  echo "ZIP code $1 is in " $location
fi

スクリプトを実行すると、次の結果が表示されます。bash getZipcode.sh 30001

getZipecode.sh: line 10: <title>Page: command not found
ZIP code 30001 is in  <title>Page not found - City-Data.com</title>

10行目はif $location = $errorResponse;簡潔さのために台本に入れた作家名とShe-Bangを削除したところです。

誰でもこの問題を解決するのに役立ちますか?

ベストアンサー1

あなたの声明が間違っていますif。以下を試してください。

baseURL="https://www.city-data.com/zips"
errorResponse=" <title>Page not found - City-Data.com</title>"

location=$( curl -s -dump "$baseURL/$1.html" | grep -i '<title>' | cut -d\( -f2 | cut -d\) -f1 )

if [ "$location" = "$errorResponse" ];
then
  echo "That zipcode does not exist"
  exit 0
else
  echo "ZIP code $1 is in " $location
fi

問題は、ifステートメントを実行するときにプログラムが変数の内容をパスのシステムコマンドであるかのように実行しようとすることです。

Bashで文字列を比較する方法の詳細については、以下を確認してください。ここ

おすすめ記事