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) ?
Asked
Active
Viewed 75 times
0

Yu Hao
- 119,891
- 44
- 235
- 294

NeronLeVelu
- 9,908
- 1
- 23
- 43
-
More appropriate on http://unix.stackexchange.com/ – Yu Hao Nov 10 '14 at 10:23
-
1Didn't get your question. What is problem in `command 2>&1`? – anubhava Nov 10 '14 at 10:36
-
Or the combined `&> file`? – Etan Reisner Nov 10 '14 at 11:23
-
`2>&1` only redirect stdout to stdout but you loose stdout in this case. I try to have the stream to stdout AND on stderr at the same time (duplicate the output, one on each channel) but without using temporary obejct (variable or file) – NeronLeVelu Nov 10 '14 at 11:34
-
@Etan Reisner: it's just the opposite of `&>` where all the stream goes to 1 stream (and it doesn not work on KSH like in BASH) – NeronLeVelu Nov 10 '14 at 13:08
2 Answers
1
#!/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]

Walter A
- 19,067
- 2
- 23
- 43
-
ok, quite simple batch, good idea. Just adapt the read to allow `\\` with a -r and maybe use a printf instead of echo to avoid interpretation by shell – NeronLeVelu Nov 28 '14 at 10:29
0
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'

Community
- 1
- 1

NeronLeVelu
- 9,908
- 1
- 23
- 43