Create a pipe that writes to multiple files (tee)

后端 未结 5 1700
情话喂你
情话喂你 2021-02-09 14:52

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 EVE         


        
5条回答
  •  心在旅途
    2021-02-09 15:31

    I worked out a solution using 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
    

提交回复
热议问题