exit 1
SIGHUP を送信しません。リターン コード (AKA 終了ステータス) で終了します。 1. SIGHUP を送信するには、次を使用しますkill
。
#!/bin/bash
# capture an interrupt # 0
trap 'echo "Signal 0 detected..."' 0
trap 'echo "SIGHUP detected..."' SIGHUP
# display something
echo "This is a checkpoint 1"
kill -1 $$
echo "This is checkpoint 2"
# exit shell script with 0 signal
exit 0
$$
現在のプロセスの ID です。したがって、kill -1 $$
現在のプロセスにシグナル 1 (SIGHUP) を送信します。上記のスクリプトの出力は次のとおりです。
This is a checkpoint 1
SIGHUP detected...
This is checkpoint 2
Signal 0 detected...
終了時にリターン コードを確認する方法
特別なシグナルをキャッチするのではなく、戻りコード (終了ステータスとも呼ばれます) をチェックすることが目標である場合、$?
終了時にステータス変数をチェックするだけで済みます。
#!/bin/bash
# capture an interrupt # 0
trap 'echo "EXIT detected with exit status $?"' EXIT
echo "This is checkpoint 1"
# exit shell script with 0 signal
exit "$1"
echo "This is checkpoint 2"
コマンド ラインで実行すると、次のようになります。
$ status_catcher 5
This is checkpoint 1
EXIT detected with exit status 5
$ status_catcher 208
This is checkpoint 1
EXIT detected with exit status 208
trap ステートメントは、さまざまな戻りコードを異なる方法で処理するために任意に複雑なステートメントを含む可能性のある bash 関数を呼び出すことができることに注意してください。