675

我知道如何使用teeto 编写输出(标准输出aaa.shbbb.out同时仍将其显示在终端中:

./aaa.sh | tee bbb.out

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

4

12 回答 12

940

我假设您仍然希望在终端上看到 STDERR 和 STDOUT。您可以寻求 Josh Kelley 的答案,但我发现tail在后台保留一个周围的环境会输出您的日志文件非常笨拙和笨拙。请注意您需要如何保留一个额外的 FD,然后通过杀死它来进行清理,从技术上讲,应该在trap '...' EXIT.

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

只有,而不是仅仅将它用于您的标准输出,有一个用于标准输出的三通和一个用于标准错误。你将如何做到这一点?进程替换和文件重定向:

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

让我们分解并解释一下:

> >(..)

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

第二个也是一样的:

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

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

http://mywiki.wooledge.org/BashGuide/InputAndOutput

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


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

out="${TMPDIR:-/tmp}/out.$$" err="${TMPDIR:-/tmp}/err.$$"
mkfifo "$out" "$err"
trap 'rm "$out" "$err"' EXIT
tee -a stdout.log < "$out" &
tee -a stderr.log < "$err" >&2 &
command >"$out" 2>"$err"
于 2009-03-28T07:54:59.473 回答
847

简单地:

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

这只是将标准错误重定向到标准输出,因此 tee 回显到日志和屏幕。也许我错过了一些东西,因为其他一些解决方案看起来真的很复杂。

注意:从 Bash 版本 4 开始,您可以|&使用2>&1 |:

./aaa.sh |& tee -a log
于 2011-08-09T05:02:55.287 回答
77

这对于通过 Google 找到此内容的人可能很有用。只需取消注释您想尝试的示例。当然,您可以随意重命名输出文件。

#!/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}
于 2013-10-09T18:21:29.733 回答
23

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

./aaa.sh 2>ccc.out | 发球台./bbb.out

编辑:要将 stderr 和 stdout 都显示到屏幕并将两者都保存到文件中,您可以使用 bash 的I/O 重定向

#!/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
于 2009-03-28T01:57:50.160 回答
23

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

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

另请参阅如何 grep 标准错误流 (stderr)?你什么时候会使用额外的文件描述符?

在 bash(以及 ksh 和 zsh)中,但在 dash 等其他 POSIX shell 中,您可以使用进程替换

./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. 在这种情况下,请改用文件描述符杂耍或 ksh/zsh。

于 2013-02-06T19:24:21.103 回答
16

如果使用 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

于 2009-03-28T02:00:05.490 回答
7

如果您使用的是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

尝试多次重定向时执行隐式tees 或s(请参阅Redirection)。cat

于 2020-01-27T18:42:02.900 回答
5

在我的例子中,一个脚本正在运行命令,同时将 stdout 和 stderr 都重定向到一个文件,例如:

cmd > log 2>&1

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

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

有了上述内容,日志将拥有两者的副本,stdout并且stderr我可以从我的脚本中捕获stderr而不必担心stdout.

于 2017-05-31T12:41:38.760 回答
5

以下将适用于进程替换不可用的 KornShell (ksh),

# create a combined (standard input and standard output) collector
exec 3 <> combined.log

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

# cleanup collector
exec 3>&-

这里真正的技巧是2>&1 1>&3在我们的例子中将标准错误重定向到标准输出并将标准输出重定向到文件描述符3 的序列。此时标准错误和标准输出还没有结合。

实际上,标准错误(作为标准输入)被传递到tee它记录到的位置stderr.log,并重定向到文件描述符 3。

文件描述符 3 一直在记录它combined.log。所以combined.log包含标准输出和标准错误。

于 2017-07-31T23:01:01.083 回答
1

就像lhunath 很好解释的接受的答案一样,您可以使用

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

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

让我以马修-威尔科克森为例。

对于那些“眼见为实”的人来说,一个快速测试:

(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好像没问题。如果我重新启动它,输出可能会有所不同......

所以为什么 ?

因为,就像这里解释的那样

请注意,在 bash 中,此命令会在 [first command] 完成后立即返回,即使 tee 命令仍在执行(ksh 和 zsh 会等待子进程)

所以,如果你使用 bash,最好使用这个其他答案中给出的更好的例子:

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

它将解决以前的问题。

现在,问题是,如何检索退出状态码?
$?不起作用。
我没有找到比使用set -o pipefailset +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
于 2020-11-12T11:05:59.063 回答
0

发送到标准错误 ( STDERR) 的编译错误可以通过以下方式重定向或保存到文件中:

重击:

gcc temp.c &> error.log

C 外壳( csh):

% gcc temp.c |& tee error.log

请参阅:如何将编译/构建错误重定向到文件?

于 2021-10-22T13:05:32.250 回答
0

感谢lhunath在 POSIX 中的回答。

这是我在 POSIX 中需要正确修复的更复杂的情况:

# Start script main() function
# - We redirect standard output to file_out AND terminal
# - We redirect standard error to file_err, file_out AND terminal
# - Terminal and file_out have both standard output and standard error, while file_err only holds standard error

main() {
    # my main function
}

log_path="/my_temp_dir"
pfout_fifo="${log_path:-/tmp}/pfout_fifo.$$"
pferr_fifo="${log_path:-/tmp}/pferr_fifo.$$"

mkfifo "$pfout_fifo" "$pferr_fifo"
trap 'rm "$pfout_fifo" "$pferr_fifo"' EXIT

tee -a "file_out" < "$pfout_fifo" &
    tee -a "file_err" < "$pferr_fifo" >>"$pfout_fifo" &
    main "$@" >"$pfout_fifo" 2>"$pferr_fifo"; exit
于 2021-12-01T21:43:29.777 回答