bashスクリプトにおけるset-eとset-o pipefailの役割を説明する
mansetの説明:
set [--abefhkmnptuvxBCEHPT] [-o option] [arg ...] set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...]... ... -e Exit immediately if a pipeline (which may consist of a single simple command), a subshell command enclosed in parentheses, or one of the commands executed as part of a command list enclosed by braces (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or ││ list except the command following the final && or ││, any command in a pipeline but the last, or if the command’s return value is being inverted with !. A trap on ERR, if set, is executed before the shell exits. This option applies to the shell environment and each subshell envi- ronment separately (see COMMAND EXECUTION ENVIRONMENT above), and may cause subshells to exit before executing all the commands in the subshell.... ...
-o option-name The option-name can be one of the following:... ...
pipefail If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default... ...
個人的な理解:
set-eは、スクリプトにコマンドの戻り値が0以外の場合、スクリプトはすぐに終了し、後続のコマンドは実行されないことを示します.
set-o pipefailは、パイプ接続のコマンドシーケンスにおいて、いずれかのコマンドが0以外の値を返す限り、最後のコマンドが0以外の値を返すとしても、パイプ全体が0以外の値を返すことを示す.
小実験:
[root@desktop2 ~]# ./testset.sh
disable exit on non-zero return status and pipefail track
./testset.sh: line 6: 1/0: division by 0 (error token is "0")
return status = 0
disable exit on non-zero return status but enable pipefail track
./testset.sh: line 12: 1/0: division by 0 (error token is "0")
return status = 1
enable exit on non-zero return status and pipefail track
./testset.sh: line 18: 1/0: division by 0 (error token is "0")
[root@desktop2 ~]#
REF:
1. pipefail
http://petereisentraut.blogspot.com/2010/11/pipefail.html
2.linuxのsetコマンド:「set-e」と「set-o pipefail」
http://blog.chinaunix.net/uid-15007890-id-3493077.html
set [--abefhkmnptuvxBCEHPT] [-o option] [arg ...] set [+abefhkmnptuvxBCEHPT] [+o option] [arg ...]... ... -e Exit immediately if a pipeline (which may consist of a single simple command), a subshell command enclosed in parentheses, or one of the commands executed as part of a command list enclosed by braces (see SHELL GRAMMAR above) exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or ││ list except the command following the final && or ││, any command in a pipeline but the last, or if the command’s return value is being inverted with !. A trap on ERR, if set, is executed before the shell exits. This option applies to the shell environment and each subshell envi- ronment separately (see COMMAND EXECUTION ENVIRONMENT above), and may cause subshells to exit before executing all the commands in the subshell.... ...
-o option-name The option-name can be one of the following:... ...
pipefail If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default... ...
個人的な理解:
set-eは、スクリプトにコマンドの戻り値が0以外の場合、スクリプトはすぐに終了し、後続のコマンドは実行されないことを示します.
set-o pipefailは、パイプ接続のコマンドシーケンスにおいて、いずれかのコマンドが0以外の値を返す限り、最後のコマンドが0以外の値を返すとしても、パイプ全体が0以外の値を返すことを示す.
小実験:
#!/bin/bash
# testset.sh
echo 'disable exit on non-zero return status and pipefail track'
set +e
set +o pipefail
a=$[1/0]|b=2
echo 'return status = '$?
echo 'disable exit on non-zero return status but enable pipefail track'
set +e
set -o pipefail
a=$[1/0]|b=2
echo 'return status = '$?
echo 'enable exit on non-zero return status and pipefail track'
set -e
set -o pipefail
a=$[1/0]|b=2
echo 'return status = '$?
出力結果:[root@desktop2 ~]# ./testset.sh
disable exit on non-zero return status and pipefail track
./testset.sh: line 6: 1/0: division by 0 (error token is "0")
return status = 0
disable exit on non-zero return status but enable pipefail track
./testset.sh: line 12: 1/0: division by 0 (error token is "0")
return status = 1
enable exit on non-zero return status and pipefail track
./testset.sh: line 18: 1/0: division by 0 (error token is "0")
[root@desktop2 ~]#
REF:
1. pipefail
http://petereisentraut.blogspot.com/2010/11/pipefail.html
2.linuxのsetコマンド:「set-e」と「set-o pipefail」
http://blog.chinaunix.net/uid-15007890-id-3493077.html