BSD 风格的 UNIX 上的 ksh。如何将 stderr 重定向到 tee 和文件
ksh on BSD style UNIX. How to redirect stderr to tee and a file
有人问过类似的问题here
但它对我不起作用
"shell.ksh" > >(tee here.log ) 2> >(tee err.log)
ksh: 0403-057 Syntax error: `>' is not expected.
这个命令不会重定向 stderr 只是 stdop
"shell.ksh" | tee file.log 2>&1
我在 AIX 运行 一些类似于 BSD 的 UNIX,没有 GNU 扩展
这是 ksh
ps $$
PID TTY STAT TIME COMMAND
40632390 pts/6 A 0:00 -ksh
任何简短而甜蜜的解决方案?
这是一个包装器脚本,可以执行您想要的操作。我无法确认它是否适用于 AIX ksh,但它可以使用 sh (AT&T Research) 93u+ 2012-08-01
(Debian 的 ksh
包将其称为 "Real, AT&T version of the Korn shell." 这不是故意的 pdksh
nor its mksh
分支)。
这是便携式的,应该适用于所有 POSIX 外壳。
管道 1+2.ksh
#!/bin/ksh
touch err.log here.log # create the logs now so tail -f doesn't complain
tail -f here.log & # follow the output we'll pipe to later (run in bg)
HERE=$! # save the process ID of this backgrounded process
tail -f err.log >&2 & # follow output (as above), pipe output to stderr
ERR=$! # save the process ID
"$@" > here.log 2> err.log # run given command and pipe stdout and stderr
RET=$? # save return value
sleep 1 # tail polls at 1/s, so wait one second
kill $HERE $ERR # stop following those logs
exit $RET # restore the exit code from given command
因此您将其调用为 pipe1+2.ksh shell.ksh
(假设这两个命令都在您的路径中)。
这是shell.ksh的测试版本:
#!/bin/ksh
echo this output is stdout 1
echo this output is stderr >&2
echo this output is stdout 2
和试用 运行:
# ksh pipe1+2.ksh ksh shell.ksh
this output is stdout 1
this output is stdout 2
this output is stderr
# cat here.log
this output is stdout 1
this output is stdout 2
# cat err.log
this output is stderr
注意:因为 tail -f
不是瞬时的,stdout 与 stderr 会有点乱。它甚至不会在 运行s 之间保持一致,至少 运行s 的代码与 echo
.
一样快
我怀疑你的 tail
版本是否支持它,但是 GNU tail 有一个 -s SECONDS
标志,可以让你指定一个不同的轮询间隔作为十进制值,所以你可以尝试例如tail -f -s 0.01 …
改进显示内容的顺序。如果您的 sleep
版本也支持十进制值,请更改它以匹配该数字。
(我最初根据令人惊叹的 Csh Programming Considered Harmful, but that didn't seem to work. See the comments and/or the initial version of this answer 中的 "more elaborate combinations" 之一制作了一个复杂的文件描述符管道响应。)
有人问过类似的问题here
但它对我不起作用
"shell.ksh" > >(tee here.log ) 2> >(tee err.log)
ksh: 0403-057 Syntax error: `>' is not expected.
这个命令不会重定向 stderr 只是 stdop
"shell.ksh" | tee file.log 2>&1
我在 AIX 运行 一些类似于 BSD 的 UNIX,没有 GNU 扩展 这是 ksh
ps $$
PID TTY STAT TIME COMMAND
40632390 pts/6 A 0:00 -ksh
任何简短而甜蜜的解决方案?
这是一个包装器脚本,可以执行您想要的操作。我无法确认它是否适用于 AIX ksh,但它可以使用 sh (AT&T Research) 93u+ 2012-08-01
(Debian 的 ksh
包将其称为 "Real, AT&T version of the Korn shell." 这不是故意的 pdksh
nor its mksh
分支)。
这是便携式的,应该适用于所有 POSIX 外壳。
管道 1+2.ksh
#!/bin/ksh
touch err.log here.log # create the logs now so tail -f doesn't complain
tail -f here.log & # follow the output we'll pipe to later (run in bg)
HERE=$! # save the process ID of this backgrounded process
tail -f err.log >&2 & # follow output (as above), pipe output to stderr
ERR=$! # save the process ID
"$@" > here.log 2> err.log # run given command and pipe stdout and stderr
RET=$? # save return value
sleep 1 # tail polls at 1/s, so wait one second
kill $HERE $ERR # stop following those logs
exit $RET # restore the exit code from given command
因此您将其调用为 pipe1+2.ksh shell.ksh
(假设这两个命令都在您的路径中)。
这是shell.ksh的测试版本:
#!/bin/ksh
echo this output is stdout 1
echo this output is stderr >&2
echo this output is stdout 2
和试用 运行:
# ksh pipe1+2.ksh ksh shell.ksh
this output is stdout 1
this output is stdout 2
this output is stderr
# cat here.log
this output is stdout 1
this output is stdout 2
# cat err.log
this output is stderr
注意:因为 tail -f
不是瞬时的,stdout 与 stderr 会有点乱。它甚至不会在 运行s 之间保持一致,至少 运行s 的代码与 echo
.
我怀疑你的 tail
版本是否支持它,但是 GNU tail 有一个 -s SECONDS
标志,可以让你指定一个不同的轮询间隔作为十进制值,所以你可以尝试例如tail -f -s 0.01 …
改进显示内容的顺序。如果您的 sleep
版本也支持十进制值,请更改它以匹配该数字。
(我最初根据令人惊叹的 Csh Programming Considered Harmful, but that didn't seem to work. See the comments and/or the initial version of this answer 中的 "more elaborate combinations" 之一制作了一个复杂的文件描述符管道响应。)