gpt4 book ai didi

shell - 创建一个写入多个文件的管道 (tee)

转载 作者:行者123 更新时间:2023-12-04 13:55:36 28 4
gpt4 key购买 nike

我想在 ksh 脚本(使用 exec)中创建一个管道,该管道连接到 tee,并将输出发送到管道。

当前:

#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

我怎样才能创建这个管道?

最佳答案

我使用 named pipes 找到了一个解决方案.

#!/bin/ksh

LOG=~/testLog.log
PIPE=~/logPipe
mkfifo ${PIPE}
exec 3>&1 #Save STDOUT as 3
exec 4>&2 #Save STDERR as 4
tee -a ${LOG} <${PIPE} >&3 & #Start tee off the logpipe in the background
exec 1>${PIPE} #Redirect stdout to the pipe
exec 2>&1 #Redirect STDERR to STDOUT

echo "TEST"
echo Test 2

ls | grep -i "test"

rm -f ${PIPE} #Remove the pipe

关于shell - 创建一个写入多个文件的管道 (tee),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2288939/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com