ksh 将 stdout 和 stderr 重定向到不同的进程
如何在 UNIX 上重定向 stdout 管道和 stderr 管道 nn ksh? (不是Linux)。
以下方法可以重定向到文件:
x=0
while [[ "$x" -lt 100 ]]; do
echo "junk" >&2
echo "$x log test"
echo "junk err" &>2
echo "$x err test" >&2
x=$(($x+1))
done 2>out1.err 1>out1.log
我尝试过将管道重定向到其他进程,如下所示,但这不起作用:
x=0
while [[ "$x" -lt 100 ]]; do
echo "junk" >&2
echo "$x log test"
echo "junk err" &>2
echo "$x err test" >&2
x=$(($x+1))
done 2>&3 | sort -u > out2.log 3>&1 | sort -u > out2.err
How can one redirect stdout pipes and stderr pipes nn ksh on UNIX? (Not Linux).
The following works to redirect to files:
x=0
while [[ "$x" -lt 100 ]]; do
echo "junk" >&2
echo "$x log test"
echo "junk err" &>2
echo "$x err test" >&2
x=$(($x+1))
done 2>out1.err 1>out1.log
I've tried things to redirect pipes to other processes, like the following but this doesn't work:
x=0
while [[ "$x" -lt 100 ]]; do
echo "junk" >&2
echo "$x log test"
echo "junk err" &>2
echo "$x err test" >&2
x=$(($x+1))
done 2>&3 | sort -u > out2.log 3>&1 | sort -u > out2.err
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
进程替换:
Process substitution:
这是您的代码
2>&3 | 的问题排序 -u > out2.log 3>&1
。在命令完全执行之前,管道不会链接 FD 或文件。例如,考虑以下文件 bla:(
注意:在执行以下命令之前,名为 blu 不存在的文件。)
现在,如果您列出目录,您将看到一个蓝色创建。那是因为猫bla> blu 正确执行,但在执行整个命令之前没有创建文件。因此,当 sort 尝试读取 blu 时,它会抛出错误,因为它不存在。不要犯滥用“|”的错误对于一个“;” 。他们是完全不同的。
希望这有帮助。
This is problem with your code
2>&3 | sort -u > out2.log 3>&1
. A pipe will not link an FD or a file until the command is completely executed.For example , consider the following file bla:
(NOTE: file called blu DOES NOT EXIST before I execute the following command.)
Now if you list the directory u will see a blu created. That is because cat bla > blu executed correctly but no file was made until the entire command was executed. hence when sort tries to read blu it throws an error coz it doesn't exist. Do no make the mistake of misusing "|" for a ";" . They are completely different.
Hope this helped.