解释bash命令"exec>>(tee $ LOG_FILE)2>& 1" [英] Explain the bash command "exec > >(tee $LOG_FILE) 2>&1"

查看:90
本文介绍了解释bash命令"exec>>(tee $ LOG_FILE)2>& 1"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是将bash脚本的所有输出显示在控制台上并记录到文件中.

My intent was to have all the output of my bash script displayed on the console and logged to a file.

这是我的脚本,可以正常工作.

Here is my script that works as expected.

#!/bin/bash

LOG_FILE="test_log.log"
touch $LOG_FILE

# output to console and to logfile
exec > >(tee $LOG_FILE) 2>&1

echo "Starting command ls"
ls -al
echo "End of script"

但是我不明白为什么会这样.

However I do not understand why it works that way.

我希望有 exec>>(tee $ LOG_FILE)2>& 1 工作,但是尽管 exec>> $ LOG_FILE 2>& 1 确实有效.

I expected to have exec >>(tee $LOG_FILE) 2>&1 work but it fails although exec >>$LOG_FILE 2>&1 indeed works.

我找不到构造 exec>的原因.>(命令) bash手册中也不在高级bash脚本中.您能解释其背后的逻辑吗?

I could not find the reason for the construction exec > >(command ) in the bash manual nor in advanced bash scripting. Can you explain the logic behind it ?

推荐答案

>(tee $ LOG_FILE)进程替换的示例,您可能希望寻找那个.高级Shell脚本

The >(tee $LOG_FILE) is an example of Process substitution, you might wish to search for that. Advanced Shell Scriptng and Bash manual

使用语法<(程序)捕获输出,并使用>(程序)馈送输入,我们一次只能传递一条记录.它比命令替换(反引号或 $())更强大,因为它代替了文件名,而不是文本.因此,通常在指定文件的任何地方,我们都可以替换程序的标准输出或输入(尽管对输入进行进程替换并不常见).当程序没有为您想要的内容使用标准流时,此功能特别有用.

Using the syntax, <(program) for capturing output and >(program) for feeding input, we can pass data just one record at a time. It is more powerful than command substitution (backticks, or $( )) because it substitutes for a filename, not text. Therefore anywhere a file is normally specified we can substitute a program's standard output or input (although process substitution on input is not all that common). This is particularly useful where a program does not use standard streams for what you want.

请注意,在您的示例中,您缺少空格, exec>>(tee $ LOG_FILE)2>& 1 错误(您会得到一个语法错误),

Note that in your example you are missing a space, exec >>(tee $LOG_FILE) 2>&1 is wrong (you will get a syntax error),

exec > >(tee $LOG_FILE) 2>&1

是正确的,空格很关键.

is correct, that space is critical.

因此, exec> 部分更改了文件描述符1(默认值),也称为 stdout 标准输出,以供参考替换为接下来发生的一切",在这种情况下,它是进程替换,尽管通常它是文件名.

So, the exec > part changes file descriptor 1 (the default), also known as stdout or standard output, to refer to "whatever comes next", in this case it is the process substitution, although normally it would be a filename.

2>& 1 重定向文件描述符2, stderr 标准错误指向与文件描述符1相同的位置(如果您省略了& ,最后得到的文件名为 1 ).

2>&1 redirect file descriptor 2, stderr or standard error to refer to the same place as file descriptor 1 (if you omit the & you end-up with a file called 1).

完成此操作后,您便会更改当前进程的标准输出,因此后续命令的输出将进入该 tee 进程.

Once you have done that, then you have changed the current process's standard output, so output from the commands which follow go to that tee process.

这篇关于解释bash命令"exec&gt;&gt;(tee $ LOG_FILE)2&gt;&amp; 1&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆