小ネタです。
シェルスクリプトを書いている際にwgetで正常にファイルをダウンロードできた場合のみ処理を継続したい場合には次のようなコードを書きます。
#!/bin/bash wget 'https://example.com/foo.zip' if [ $? -ne 0 ]; then echo "[ERROR] 正常にダウンロードできませんでした" exit 1 fi
$?は直前のコマンドの実行結果が自動的に入ります。- 比較演算子の
-neはnot equalの略で、値が等しくない場合に使用します。
ステータスコード一覧
Linuxのコマンドは大抵そうなのですが、0は成功、0以外が何らかのエラーとなります。wgetもこのルールに従っています。
| 値 | 説明 |
|---|---|
| 0 | No problems occurred. |
| 1 | Generic error code. |
| 2 | Parse error—for instance, when parsing command-line options, the ‘.wgetrc’ or ‘.netrc’... |
| 3 | File I/O error. |
| 4 | Network failure. |
| 5 | SSL verification failure. |
| 6 | Username/password authentication failure. |
| 7 | Protocol errors. |
| 8 | Server issued an error response. |
詳細は公式ドキュメントを参照ください。 www.gnu.org


