开发者

ksh redirect stdout and stderr to different processes

开发者 https://www.devze.com 2023-03-16 02:57 出处:网络
How can one redirect stdout pipes and stderr pipes nn ksh on UNIX? (Not Linux). The following works to redirect to files:

How can one redirect stdout pipes and stderr pipes nn ksh on UNIX? (Not Linux).

The following works to redirect to files:

x=0
while [[ "$x" -lt 100 ]]; do
    echo "junk" >&2
    echo "$x log test"
    echo "junk err" &>2
    echo "$x err test开发者_开发百科" >&2
    x=$(($x+1))
done 2>out1.err 1>out1.log

I've tried things to redirect pipes to other processes, like the following but this doesn't work:

x=0
while [[ "$x" -lt 100 ]]; do
    echo "junk" >&2
    echo "$x log test"
    echo "junk err" &>2
    echo "$x err test" >&2
    x=$(($x+1))
done 2>&3 | sort -u > out2.log 3>&1 | sort -u > out2.err


Process substitution:

some_process 1> >(do_something_with stdout) 2> >(do_something_else_with stderr)


This is problem with your code 2>&3 | sort -u > out2.log 3>&1. A pipe will not link an FD or a file until the command is completely executed.

For example , consider the following file bla:

bash-4.0$ cat bla
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

(NOTE: file called blu DOES NOT EXIST before I execute the following command.)

bash-4.0$ cat bla > blu | sort < blu

bash: blu: No such file or directory

Now if you list the directory u will see a blu created. That is because cat bla > blu executed correctly but no file was made until the entire command was executed. hence when sort tries to read blu it throws an error coz it doesn't exist. Do no make the mistake of misusing "|" for a ";" . They are completely different. Hope this helped.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号