创建写入多个文件的管道 (tee)
我想在 ksh 脚本中创建一个管道(使用 exec),将管道连接到三通,并将输出发送到管道。
当前:
#Redirect EVERYTHING
exec 3>&1 #Save STDOUT as 3
exec 4>&2 #Save STDERR as 4
exec 1>${Log} #Redirect STDOUT to a log
exec 2>&1 #Redirect STDERR to STDOUT
我想要做什么(但我的语法不正确):
#Redirect EVERYTHING
exec 3>&1 #Save STDOUT as 3
exec 4>&2 #Save STDERR as 4
exec 1>tee -a ${Log} >&3 #Redirect STDOUT to a log
exec 2>&1 #Redirect STDERR to STDOUT
如何创建此管道?
I would like to create a pipe in a ksh script (using exec) that pipe's to a tee, and sends the output to a pipe.
Current:
#Redirect EVERYTHING
exec 3>&1 #Save STDOUT as 3
exec 4>&2 #Save STDERR as 4
exec 1>${Log} #Redirect STDOUT to a log
exec 2>&1 #Redirect STDERR to STDOUT
What'd I'd like to do (but I don't have the syntax correct):
#Redirect EVERYTHING
exec 3>&1 #Save STDOUT as 3
exec 4>&2 #Save STDERR as 4
exec 1>tee -a ${Log} >&3 #Redirect STDOUT to a log
exec 2>&1 #Redirect STDERR to STDOUT
How can I create this pipe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我使用命名管道制定了一个解决方案。
I worked out a solution using named pipes.
这是我使用的解决方案。它在我的 Mac 上的 ksh 下运行。它很好地封装在 start_logging() 和 stop_logging() 函数中,使生活变得轻松。
代码在实践中看起来像这样:
这是整个文件。启动和停止函数以及上面的示例都位于文件的底部。为了使其更易于使用,只需将启动和停止函数放在自己的文件中,并将它们源到需要日志记录的脚本中。
Here's a solution I use. It works under ksh on my Mac. It's nicely encapsulated into start_logging() and stop_logging() functions to make life easy.
The code looks like this in practice:
Here is the whole file. The start and stop functions along with the example above are all at the bottom of the file. To make it easier to use, just put the start and stop functions in their own file and source them in the scripts where you need the logging.
我知道 bash 不是 ksh,但是有很多重叠,所以也许这也适用于那里。
创建一个运行 process2 的子 shell。该子 shell 将来自 process1 的文件描述符 N 的数据作为其标准输入接收。因此,特别是,您可以这样做:
我不知道如果将
process1
替换为exec,但你可以尝试一下。
I know bash not ksh, but there's a lot of overlap, so maybe this will work there too.
Creates a subshell running process2. That subshell receives as its stdin the data from process1's file descriptor N. So in particular, you could do:
I don't know whether this would also work if
process1
is replaced withexec
, but you could give it a try.ksh 中有
|&
和>&p
,但我无法让它们做你正在寻找的事情。也许你可以。There are
|&
and>&p
in ksh, but I couldn't get them to do what you're looking for. Maybe you can.而不是:
exec 1>tee -a ${Log} >&3
简单地执行:
tee -a ${Log} >&3 &
< code>tee 将分叉到后台,并将消耗调用进程的“(即您的脚本的)STDIN,就像
tee
分叉时一样。Instead of:
exec 1>tee -a ${Log} >&3
do simply:
tee -a ${Log} >&3 &
tee
will fork into the background, and will consume the calling process' (i.e. your script's) STDIN as it was at the time thattee
forked.