`set -x`会导致Bash输出到哪里?

huangapple go评论72阅读模式
英文:

Where does `set -x` cause bash to print to?

问题

如果我在我的bash会话中使用set -x(v4.1.2(2) - CentOS 6.10),我会得到:

$ ls /root
+ ls --color=auto /root
ls: cannot open directory /root: Permission denied

很好,它会回显我运行的命令并将其打印到终端。这是预期的。现在,如果我将stdoutstderr都重定向到另一个文件。

$ ls /root &> stuff.txt
+ ls --color=auto /root

它仍然会将命令打印到终端。

问题

如果set -x不是将输出打印到stderrstdout,那么它会将输出打印到哪里?

英文:

If I set -x in my bash session ( v4.1.2(2) - CentOS 6.10), I get :

$ ls /root
+ ls --color=auto /root
ls: cannot open directory /root: Permission denied

Great, it echo's the command I ran and prints out the terminal. This is expected. Now if I redirect both stdout and stderr to the another file.

$ ls /root  &> stuff.txt
+ ls --color=auto /root

It still prints the command to the terminal.

QUESTION

Where is set -x having bash print to if it isn't stderr or stdout?

答案1

得分: 12

The set -x command prints tracing information to stderr.

When you run this command...

ls /root  &> stuff.txt

You're only redirecting stdout and stderr for the ls command. You're not changing either for your current shell, which is where you have run set -x.


As Mad Physicist points out, the technical answer is "it logs to BASH_XTRACEFD", which defaults to stderr. You can redirect trace logging for the current shell to another file by doing something like:

# open a new file descriptor for logging
exec 4> trace.log

# redirect trace logs to fd 4
BASH_XTRACEFD=4

# enable tracing
set -x
英文:

The set -x command prints tracing information to stderr.

When you run this command...

ls /root  &> stuff.txt

You're only redirecting stdout and stderr for the ls command. You're not changing either for your current shell, which is where you have run set -x.


As Mad Physicist points out, the technical answer is "it logs to BASH_XTRACEFD", which defaults to stderr. You can redirect trace logging for the current shell to another file by doing something like:

# open a new file descriptor for logging
exec 4> trace.log

# redirect trace logs to fd 4
BASH_XTRACEFD=4

# enable tracing
set -x

答案2

得分: 3

当您执行命令时,您可以将命令的标准输出(称为 /dev/stdout)直接重定向到文件。如果命令生成错误输出(通常发送到 /dev/stderr),您也可以将其重定向到文件,如下所示:

$ command > /path/to/output.txt 2> /path/to/error.txt

当您执行命令 set -x 时,您要求它生成正在执行的命令的 跟踪。它通过将消息发送到 /dev/stderr 来执行此操作。与普通命令不同,您不能以类似普通命令的方式轻松重定向它。这是因为 bash 执行脚本并同时将跟踪信息发送到 /dev/stderr。因此,如果您想捕获跟踪信息,您需要直接重定向 bash 的错误输出。可以通过以下命令完成:

exec 2> /path/to/trace.txt

注意: 这同时还会包含脚本中执行的任何命令的所有错误输出。

示例:

#!/usr/bin/env bash
set -x
command

这将所有输出和错误输出发送到终端。

#!/usr/bin/env bash
set -x
command 2> /path/to/command.err

这将 command 的输出和 bash 的跟踪信息发送到终端,但将 command 的错误输出捕获到文件中。

#!/usr/bin/env bash
set -x
exec 2> /path/to/trace.err
command 2> /path/to/command.err

这将 command 的输出发送到终端,将 command 的错误输出发送到文件,将脚本的跟踪信息发送到 /path/to/trace.err

#!/usr/bin/env bash
set -x
exec 2> /path/to/trace_and_command.err
command

这将 command 的输出发送到终端,将 command 的跟踪信息和错误发送到文件。

英文:

When you execute a command, you can redirect the standard output (known as /dev/stdout) of the command directly to the file. Also if the command generates error-output (generally send to /dev/stderr) you can also redirect it to a file as:

$ command > /path/to/output.txt 2> /path/to/error.txt

When you execute the command set -x, you ask it to generate a trace of the commands being executed. It does this by sending messages to /dev/stderr. In contrast to a normal command, you cannot easily redirect this in a similar way as with a normal command. This is because bash executes the script and at the same time generates the trace to /dev/stderr. So if you would like to catch the trace, you would have to redirect the error output of bash directly. This can be done by the command

 exec 2> /path/to/trace.txt

note: this will at the same time also contain all the error output of any command executed in the script.

Examples:

#!/usr/bin/env bash
set -x
command

This sends all output and error output to the terminal

#!/usr/bin/env bash
set -x
command 2> /path/to/command.err

This sends the output of command and the trace of bash to the terminal but catches the error output of command in a file

#!/usr/bin/env bash
set -x
exec 2> /path/to/trace.err
command 2> /path/to/command.err

This sends the output of command to the terminal, the error output of command to a file, and the trace of the script to /path/to/trace.err

#!/usr/bin/env bash
set -x
exec 2> /path/to/trace_and_command.err
command

This sends the output of command to the terminal, the trace and the error of command to a file.

huangapple
  • 本文由 发表于 2020年1月3日 22:10:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定