スクリプト
#!/usr/bin/env bash
# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Exit on error inside any functions or subshells.
set -o errtrace
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip`
set -o pipefail
# Turn on traces, useful while debugging but commented out by default
set -o xtrace
bash_backtrace() {
echo TEST
ls -l /proc/$$/fd >&2
}
trap bash_backtrace ERR
CMD="ls /does-not-exist"
eval "${CMD}" > /tmp/foo
exit
出力
$ ./test.sh
+ trap bash_backtrace ERR
+ CMD='ls /does-not-exist'
+ eval 'ls /does-not-exist'
++ ls /does-not-exist
ls: cannot access /does-not-exist: No such file or directory
+++ bash_backtrace
+++ echo TEST
+++ ls -l /proc/19650/fd
total 0
lrwx------. 1 sbarre sbarre 64 Apr 18 15:57 0 -> /dev/pts/0
l-wx------. 1 sbarre sbarre 64 Apr 18 15:57 1 -> /tmp/foo
lrwx------. 1 sbarre sbarre 64 Apr 18 15:57 10 -> /dev/pts/0
lrwx------. 1 sbarre sbarre 64 Apr 18 15:57 2 -> /dev/pts/0
lr-x------. 1 sbarre sbarre 64 Apr 18 15:57 255 -> /home/sbarre/test.sh
私の評価でエラーが発生し、トラップにかかるため、stdoutはまだ/tmp/fooを指しています。したがって、私のトラップ機能のすべてのエコーは端末ではなく対応するファイルに移動します。
トラップ機能でどのように安全にリセットできますか?標準出力をリダイレクトする方法でスクリプト自体が実行される状況を処理する必要があります。
$ ./test.sh > log.txt
私はstdoutを「修正」したいと思いますlog.txt
。/tmp/foo
ベストアンサー1
これが標準エラーストリームの目的です。
bash_backtrace() {
echo TEST >&2
ls -l "/proc/$$/fd" >&2
}
トラップが診断メッセージ(TEST
)を出力しています。これは標準出力ではなく標準エラーに移動する必要があります。