如何通过管道输出跟踪的 bash 调试?

如何通过管道输出跟踪的 bash 调试?

我想通过管道传输跟踪脚本的输出,如下所示:

bash -x /path/to/a/script | more 

或者

bash -o xtrace /path/to/a/script | more

bash -x /path/to/script > a_file 

或者

bash -o xtrace /path/to/script > a_file`

但不起作用。由于它是内部文件,/etc我不想编辑它,而且由于输出太长,我无法向后滚动以查看所需的输出。

我做错了吗?

答案1

使用:

bash -x /path/to/a/script |& more

bashxtrace 输出写入 STDERR,而管道传输时|您只需将 的 STDOUT 提供给bash -x /path/to/a/scriptmore右侧的任何其他命令|

|&将通过管道将 STDOUT 和 STDERR 都传输到,more以便您可以将它们同时使用more

或者,如果您只关心管道xtrace(STDERR),请使用以下命令:

bash -x /path/to/script 2>&1 >/dev/null | more

重定向到文件时,如果要重定向脚本输出和xtrace输出,请使用以下任一方法:

bash -x /path/to/script &>/where/to/save
bash -x /path/to/script >/where/to/save 2>&1

如果您只想重定向xtrace

bash -x /path/to/script 2>/where/to/save

答案2

bash -x /path/to/script 2> a_file

应该可以完成这项工作,这样你就可以通过管道传输stderr到文件,而不仅仅是stdout

相关内容