在管道上使用“tee”时如何将stderr写入文件?

时间:2009-03-28 01:53:05

标签: linux bash unix

我知道如何使用teeSTDOUT的输出(aaa.sh)写入bbb.out,同时仍在终端中显示:

./aaa.sh | tee bbb.out

我现在如何将STDERR写入名为ccc.out的文件,同时仍然显示它?

10 个答案:

答案 0 :(得分:701)

我假设您仍想在终端上看到STDERR和STDOUT。你可以去找Josh Kelley的答案,但是我发现在后台保持tail左右,输出你的日志文件非常hackish和cludgy。注意你需要保留一个exra FD并在之后通过杀死来进行清理,技术上应该在trap '...' EXIT中进行清理。

有一种更好的方法可以做到这一点,而且您已经发现了它:tee

只是,而不是仅仅为stdout使用它,有stdout的发球台和stderr的发球台。你将如何实现这一目标?进程替换和文件重定向:

command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)

让我们分开并解释一下:

> >(..)

>(...)(进程替换)创建一个FIFO并让tee监听它。然后,它使用>(文件重定向)将command的STDOUT重定向到您的第一个tee正在侦听的FIFO。

第二个相同:

2> >(tee -a stderr.log >&2)

我们再次使用进程替换来创建一个从STDIN读取并将其转储到tee的{​​{1}}进程。 stderr.log在STDOUT上输出其输入,但由于它的输入是我们的STDERR,我们想要再次将tee的STDOUT重定向到我们的STDERR。然后我们使用文件重定向将tee的STDERR重定向到FIFO的输入(command的STDIN)。

请参阅http://mywiki.wooledge.org/BashGuide/InputAndOutput

过程替换是你选择tee作为你的shell而不是bash(POSIX或Bourne)的好处之一。


sh中,您必须手动执行操作:

sh

答案 1 :(得分:592)

为什么不简单:

./aaa.sh 2>&1 | tee -a log

这只是简单地将stderr重定向到stdout,因此Te回应记录和屏幕。也许我错过了一些东西,因为其他一些解决方案似乎很复杂。

注意:自bash版本4起,您可以使用|&作为2>&1 |的缩写:

./aaa.sh |& tee -a log

答案 2 :(得分:52)

这可能对通过谷歌发现此问题的人有用。只需取消注释您想要尝试的示例即可。当然,可以随意重命名输出文件。

#!/bin/bash

STATUSFILE=x.out
LOGFILE=x.log

### All output to screen
### Do nothing, this is the default


### All Output to one file, nothing to the screen
#exec > ${LOGFILE} 2>&1


### All output to one file and all output to the screen
#exec > >(tee ${LOGFILE}) 2>&1


### All output to one file, STDOUT to the screen
#exec > >(tee -a ${LOGFILE}) 2> >(tee -a ${LOGFILE} >/dev/null)


### All output to one file, STDERR to the screen
### Note you need both of these lines for this to work
#exec 3>&1
#exec > >(tee -a ${LOGFILE} >/dev/null) 2> >(tee -a ${LOGFILE} >&3)


### STDOUT to STATUSFILE, stderr to LOGFILE, nothing to the screen
#exec > ${STATUSFILE} 2>${LOGFILE}


### STDOUT to STATUSFILE, stderr to LOGFILE and all output to the screen
#exec > >(tee ${STATUSFILE}) 2> >(tee ${LOGFILE} >&2)


### STDOUT to STATUSFILE and screen, STDERR to LOGFILE
#exec > >(tee ${STATUSFILE}) 2>${LOGFILE}


### STDOUT to STATUSFILE, STDERR to LOGFILE and screen
#exec > ${STATUSFILE} 2> >(tee ${LOGFILE} >&2)


echo "This is a test"
ls -l sdgshgswogswghthb_this_file_will_not_exist_so_we_get_output_to_stderr_aronkjegralhfaff
ls -l ${0}

答案 3 :(得分:22)

要将stderr重定向到文件,将stdout显示到屏幕,并将stdout保存到文件中:

./aaa.sh 2>ccc.out | tee ./bbb.out

编辑:要将stderr和stdout同时显示到屏幕并同时保存到文件中,您可以使用bash的I/O redirection

#!/bin/bash

# Create a new file descriptor 4, pointed at the file
# which will receive stderr.
exec 4<>ccc.out

# Also print the contents of this file to screen.
tail -f ccc.out &

# Run the command; tee stdout as normal, and send stderr
# to our file descriptor 4.
./aaa.sh 2>&4 | tee bbb.out

# Clean up: Close file descriptor 4 and kill tail -f.
exec 4>&-
kill %1

答案 4 :(得分:15)

换句话说,您希望将stdout传递到一个过滤器(tee bbb.out)并将stderr传递到另一个过滤器(tee ccc.out)。没有标准方法可以将除stdout以外的任何东西传递到另一个命令中,但是你可以通过处理文件描述符来解决这个问题。

{ { ./aaa.sh | tee bbb.out; } 2>&1 1>&3 | tee ccc.out; } 3>&1 1>&2

另请参阅How to grep standard error stream (stderr)?When would you use an additional file descriptor?

在bash(和ksh和zsh)中,但在其他POSIX shell(如破折号)中没有,您可以使用process substitution

./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out)

请注意,在bash中,只要./aaa.sh完成,此命令就会返回,即使tee命令仍在执行(ksh和zsh等待子进程)。如果您执行./aaa.sh > >(tee bbb.out) 2> >(tee ccc.out); process_logs bbb.out ccc.out之类的操作,则可能会出现问题。在这种情况下,请使用文件描述符juggling或ksh / zsh。

答案 5 :(得分:12)

如果使用bash:

# Redirect standard out and standard error separately
% cmd >stdout-redirect 2>stderr-redirect

# Redirect standard error and out together
% cmd >stdout-redirect 2>&1

# Merge standard error with standard out and pipe
% cmd 2>&1 |cmd2

信用(不是从我的头脑回答)到这里:http://www.cygwin.com/ml/cygwin/2003-06/msg00772.html

答案 6 :(得分:4)

如果您使用的是 zsh ,则可以使用多个重定向,因此甚至不需要tee

./cmd 1>&1 2>&2 1>out_file 2>err_file

这里,您只是将每个流重定向到自己的目标文件。


完整示例

% (echo "out"; echo "err">/dev/stderr) 1>&1 2>&2 1>/tmp/out_file 2>/tmp/err_file
out
err
% cat /tmp/out_file
out
% cat /tmp/err_file
err

请注意,这需要设置MULTIOS选项(默认设置)。

MULTIOS

尝试进行多次重定向时,请执行隐式teecat(请参见Redirection)。

答案 7 :(得分:2)

就我而言,脚本在将stdout和stderr重定向到文件时运行命令,如:

cmd > log 2>&1

我需要更新它,以便在出现故障时,根据错误消息采取一些措施。我当然可以删除dup 2>&1并从脚本中捕获stderr,但随后错误消息将不会进入日志文件以供参考。虽然@lhunath接受的答案应该是相同的,但它会将stdoutstderr重定向到不同的文件,这不是我想要的,但它帮助我提出了确切的解决方案,我需要:

(cmd 2> >(tee /dev/stderr)) > log

有了上述内容,日志将包含stdoutstderr的副本,我可以从我的脚本中捕获stderr,而无需担心stdout

答案 8 :(得分:2)

以下内容适用于KornShell(ksh),其中进程替换不可用,

# create a combined(stdin and stdout) collector
exec 3 <> combined.log

# stream stderr instead of stdout to tee, while draining all stdout to the collector
./aaa.sh 2>&1 1>&3 | tee -a stderr.log 1>&3

# cleanup collector
exec 3>&-

这里的真正诀窍是2>&1 1>&3的序列,在我们的例子中,stderr重定向到stdout并将stdout重定向到描述符3 。此时,stderrstdout尚未合并。

实际上,stderr(作为stdin)会传递到tee,并在其中记录到stderr.log并重定向到描述符3.

描述符3始终将其记录到combined.log。因此combined.log包含stdoutstderr

答案 9 :(得分:0)

accepted answer well explained by lhunath一样,您可以使用

command > >(tee -a stdout.log) 2> >(tee -a stderr.log >&2)

请注意,如果您使用bash,可能会遇到一些问题

让我以matthew-wilcoxson为例。

对于那些“眼见为实”的人,进行一下快速测试:

(echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)

个人而言,当我尝试时,会得到以下结果:

user@computer:~$ (echo "Test Out";>&2 echo "Test Err") > >(tee stdout.log) 2> >(tee stderr.log >&2)
user@computer:~$ Test Out
Test Err

两个消息都没有出现在同一级别。为什么Test Out看起来像是我以前的命令一样?
提示是空白行,让我认为该过程尚未完成,当我按Enter时,此问题得以解决。
当我检查文件的内容时,可以,重定向有效。

再进行一次测试。

function outerr() {
  echo "out"     # stdout
  echo >&2 "err" # stderr
}

user@computer:~$ outerr
out
err

user@computer:~$ outerr >/dev/null
err

user@computer:~$ outerr 2>/dev/null
out

再次尝试重定向,但是具有此功能。

function test_redirect() {
  fout="stdout.log"
  ferr="stderr.log"
  echo "$ outerr"
  (outerr) > >(tee "$fout") 2> >(tee "$ferr" >&2)
  echo "# $fout content :"
  cat "$fout"
  echo "# $ferr content :"
  cat "$ferr"
}

我个人有这个结果:

user@computer:~$ test_redirect
$ outerr
# stdout.log content :
out
out
err
# stderr.log content :
err
user@computer:~$

在空行上没有提示,但是我看不到正常的输出,stdout.log内容似乎是错误的,只有stderr.log似乎可以。 如果我重新启动它,输出可能会有所不同...

那为什么呢?

因为like explained here

请注意,在bash中,即使仍然执行tee命令(ksh和zsh确实等待子进程),该命令也会在[第一个命令]完成后立即返回

因此,如果您使用bash,请使用this other answer中给出的更好的示例:

{ { outerr | tee "$fout"; } 2>&1 1>&3 | tee "$ferr"; } 3>&1 1>&2

它将解决以前的问题。

现在,问题是,如何获取退出状态代码?
$?不起作用。
我发现没有比使用set -o pipefail打开pipefail(关闭set +o pipefail并像这样使用${PIPESTATUS[0]}

更好的解决方案了。
function outerr() {
  echo "out"
  echo >&2 "err"
  return 11
}

function test_outerr() {
  local - # To preserve set option
  ! [[ -o pipefail ]] && set -o pipefail; # Or use second part directly
  local fout="stdout.log"
  local ferr="stderr.log"
  echo "$ outerr"
  { { outerr | tee "$fout"; } 2>&1 1>&3 | tee "$ferr"; } 3>&1 1>&2
  # First save the status or it will be lost
  local status="${PIPESTATUS[0]}" # Save first, the second is 0, perhaps tee status code.
  echo "==="
  echo "# $fout content :"
  echo "<==="
  cat "$fout"
  echo "===>"
  echo "# $ferr content :"
  echo "<==="
  cat "$ferr"
  echo "===>"
  if (( status > 0 )); then
    echo "Fail $status > 0"
    return "$status" # or whatever
  fi
}
user@computer:~$ test_outerr
$ outerr
err
out
===
# stdout.log content :
<===
out
===>
# stderr.log content :
<===
err
===>
Fail 11 > 0
相关问题