我知道如何使用tee将aaa.sh的输出(标准输出)写入bbb。退出,同时仍然显示在终端中:

./aaa.sh | tee bbb.out

我现在如何将标准错误写入一个名为ccc的文件。退出,同时仍然显示它?


当前回答

我假设您仍然希望在终端上看到标准错误和标准输出。你可以选择Josh Kelley的答案,但我发现在后台保持一个尾巴,输出你的日志文件非常笨拙。注意,你需要保留一个额外的文件描述符,然后通过杀死它来进行清理,从技术上讲,应该在陷阱'…'中进行清理退出。

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

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

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

让我们分开来解释:

> >(..)

>(…)(进程替换)创建一个FIFO并让tee监听它。然后,它使用>(文件重定向)将命令的标准输出重定向到您的第一个tee正在侦听的FIFO。

第二种情况也一样:

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

我们再次使用进程替换来创建一个tee进程,该进程从标准输入读取数据并将其转储到stderr.log中。Tee将它的输入输出回标准输出,但由于它的输入是我们的标准误差,我们希望将Tee的标准输出重定向到我们的标准误差。然后我们使用文件重定向将命令的标准错误重定向到FIFO的输入(tee的标准输入)。

参见输入和输出

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


在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"

其他回答

如果你使用Z shell (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 当尝试多次重定向时执行隐式tee或cats(参见重定向)。

将标准错误重定向到一个文件,将标准输出显示到屏幕上,并将标准输出保存到一个文件:

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

要在屏幕上显示标准错误和标准输出,并将它们保存到文件中,您可以使用Bash的I/O重定向:

#!/bin/bash

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

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

# Run the command; tee standard output as normal, and send standard error
# 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

感谢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

简单:

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

这只是简单地将标准错误重定向到标准输出,因此tee对日志和屏幕都进行回显。也许我遗漏了一些东西,因为其他的解看起来很复杂。

注意:从Bash版本4开始,你可以使用|&作为2>和1 |的缩写:

./aaa.sh |& tee -a log

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

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

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

在bash(和ksh和zsh)中,而不是在其他POSIX shell中,如dash,你可以使用进程替换:

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