How can I redirect the same message to both stdoud and stderr (without temporary file)

99 views Asked by At

It is easy to redirect stdout or stderr to the same output (file or one of the 2 std output) with a 1>&1,>&2 and/or >file but is there a way to send the same output to both the std output in KSH (like | tee File but with &2 as file) ?

2

There are 2 answers

1
Walter A On BEST ANSWER
#!/bin/ksh

function mytee { 
        while read -r x ; do
                print "${x}"
                print "${x}" >&2
        done
}

echo "TestMyTee
second line
and a * character" | mytee

# Really stdout and stderr?
echo "TestMyTee
second line
and a * character" | mytee 1>mystdout 2>mystderr

Edited: replaced [read] and [echo] by [read -r] and [print]

0
NeronLeVelu On

Response on Unix & Linux

Post: How can I redirect the same message to both stdoud and stderr (without temporary object)

by Stéphane Chazelas

{ your-code } | perl -pe 'print STDERR'