$./testscript.sh | grep somestuff
$ echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"
29 1
を行います含まれtestscript.shについてpipefail
を使用する:
#!/bin/bash
set -eo pipefail
#set -o pipefail causes a pipeline to produce a failure return code
#If a command fails, set -e will make the whole script exit,
cat nonexistingfile # this command fails
echo "Some Stuff"
exit 29
は
を実行します。 $ ./testscript.sh 2>/dev/null | grep Some &
[2] 7684
$ fg 2
bash: fg: job has terminated
[2]- Exit 1 ./testscript.sh 2> /dev/null | grep --color=auto Some
スクリプトが失敗したと判断した終了ステータス1が表示されます。
はcat nonexistingfile
は、あなたが持っているでしょう削除されていた:
[2]- Done ./37257668.sh 2> /dev/null | grep --color=auto Some
Disdvantage:pipefailは
方法を失敗したコマンドに固有ではないすべての終了コードのための1つを返します。 2:ソースシェルスクリプト
$ . ./testscript.sh 2>/dev/null | grep Some & #mind the dot in the beginning
$ echo "${PIPESTATUS[0]} ${PIPESTATUS[1]}"
29 0
最後の仕上げ
シェルスクリプトで失敗する単一のコマンドが疑われる場合は、テストスクリプトは、あなたは以下を行うことができます:
#no shebang
echo "Some Stuff"
ls non_existent 2>/dev/null || ls__return_value=50
はどのような方法で
$. ./testscript | grep "Some"
$if [ $ls__return_value -eq 50 ]; then echo "Error in ls"; fi
表示しています '待機しても動作しません。 –