我一直在使用 exec 5> debug_output.txt / BASH_XTRACEFD="5" 来调试脚本,但它确实在一夜之间停止工作

I've been using exec 5> debug_output.txt / BASH_XTRACEFD="5" to debug scripts but it literally stopped working overnight

我是脚本新手,所以我一直在使用:

exec 5> debug_output.txt
BASH_XTRACEFD="5"

将脚本的输出发送到 debug_output.txt。这使我能够将脚本的输出与脚本正在执行的操作进行比较,我发现这非常有帮助。昨晚我正在测试这个设置,它工作正常。我今天早上进来了,现在它不起作用了。我在完全相同的终端中使用完全相同的脚本。该脚本将 运行 并创建 debug_output.txt 文件,但它是空的。我尝试删除 debug_output.txt 和 myscript1.sh 并重新创建它,没有任何变化。重启也没用。这是我的脚本:

#!/bin/bash

exec 5> debug_output.txt
BASH_XTRACEFD="5"
PS4='$LINENO: '

var1=blah
var2=foo

echo Currently in [=13=]
echo
echo [=13=] :: var1 : $var1, var2 : $var2

export var1
echo
echo Entering myscript2.sh
./myscript2.sh
echo
echo Back in [=13=]
echo [=13=] :: var1 : $var1, var2 : $var2
echo 

这是输出:

(^)#(^)#(^)#(^)bminter@ubuntu:~/bashscripttutorial$ ./myscript1.sh 
Currently in ./myscript1.sh

./myscript1.sh :: var1 : blah, var2 : foo

Entering myscript2.sh
Currently in ./myscript2.sh

./myscript2.sh :: var1 : blah, var2 :

Back in ./myscript1.sh
./myscript1.sh :: var1 : blah, var2 : foo

(^)#(^)#(^)#(^)bminter@ubuntu:~/bashscripttutorial$

下面是脚本运行s:

之后的内容
(^)#(^)#(^)#(^)bminter@ubuntu:~/bashscripttutorial$ ls
debug_output.txt  foo  myscript1.sh  myscript1.sh~  myscript2.sh  myscript2.sh~
(^)#(^)#(^)#(^)bminter@ubuntu:~/bashscripttutorial$ cat debug_output.txt 
(^)#(^)#(^)#(^)bminter@ubuntu:~/bashscripttutorial$ 
(^)#(^)#(^)#(^)bminter@ubuntu:~/bashscripttutorial$ 

我希望我在昨晚工作时拥有 debug_output.txt 文件的内容,但我在重新启动时丢失了它。

来自 bash documentation

BASH_XTRACEFD

If set to an integer corresponding to a valid file descriptor, Bash will write the trace output generated when ‘set -x’ is enabled to that file descriptor. This allows tracing output to be separated from diagnostic and error messages. The file descriptor is closed when BASH_XTRACEFD is unset or assigned a new value. Unsetting BASH_XTRACEFD or assigning it the empty string causes the trace output to be sent to the standard error. Note that setting BASH_XTRACEFD to 2 (the standard error file descriptor) and then unsetting it will result in the standard error being closed.

因此提供的脚本中缺少 set -x

bash -xset -o errtrace