Code: Select all
cmd1 > >(cmd2) 2> >(cmd3)Can I do the ~same in POSIX sh? Without creating any temporary files?
Code: Select all
cmd1 > >(cmd2) 2> >(cmd3)Code: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
Code: Select all
$ bash --posix
bash-5.2$ ( echo yea >&1; echo nay >&2; 2>&1 1>&3 | grep nay 1>&2 ) 3>&1 | cat -n
nay
1 yeaCode: Select all
( while true; do echo yea >&1; echo nay >&2; done; 2>&1 1>&3 | grep nay 1>&2 ) 3>&1 | cat -nCode: Select all
{ cmd1 | grab_stdout; } 2>&1 | grab_stderrCode: Select all
{ { echo stdout; echo stderr 1>&2; } | rev; } 2>&1 | catCode: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
Code: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
messing with file descriptors without providing any command? So, you're basically trying to swap output streams that belong to the shell itself rather than alter the flow between children?; 2>&1 1>&3 |
It looks absurd, but apparently the redirection 3>&1 happens before creating subshell, and the subshell inherits fd 3 so it can use it for redirection, since it's already open from the outside... I really expected it to crash and burn, but () actually makes it work even though at the first glance it should make it fail.1>&3 ) 3>&1
Code: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
Code: Select all
cmd1 > >(cmd2) 2> >(cmd3)Code: Select all
( cmd1 2>&1 1>&3 | cmd3 1>&2 ) 3>&1 | cmd2If I am not just wrongZucca wrote:I'm trying the wrap my head around this.
Apparently you really need subshell. Grouping commands won't do it.
Code: Select all
>(...)pingtoo wrote:what is motive behind this.
Code: Select all
somecommand 2>&1 | messageprocessing | logger <--switches> --prio-prefixCode: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
Yes. In this case I think it's mandatory.geki wrote:If I am not just wrongyou do 2 subshells in your bash example.
Code: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
Code: Select all
TMPDIR=/tmp/mytmpd.$$
mkdir $TMPDIR
trap "rm -rf $TMPDIR" 0 1 2 3 15
mknod $TMPDIR/out p
mknod $TMPDIR/err p
(cmd2 < $TMPDIR/out)&
(cmd3 < $TMPDIR/err)&
exec 1>$TMPDIR/out
exec 2>$TMPDIR/err
cmd1()
{
echo "normal message"
echo "error message" >2
}
cmd1Yes. It should work.pingtoo wrote:I believe my example code can work in any unix/linux environment with posix shell.
Now I'm left to wonder why bash (--posix) so often complained about bad fd 3 even when I had both ends open...[post=8794427]geki[/post] wrote:posix shellCode: Select all
( cmd1 2>&1 1>&3 | cmd3 1>&2 ) 3>&1 | cmd2
Code: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
I've been playing the same game with the same results. Works fine. I guess if you write to syslog you should get decent timestamps (though of course the ts is for when logger sees the message, not when the message was created, but that's always an insoluble problem here).Zucca wrote:pingtoo wrote:what is motive behind this....
- To grab normal informative messages and error messages from a program as separate streams
- process them
- pass to logger with appropriate arguments
Code: Select all
( sample 2>&3 | cat 1>&2 ) 3>&1 | cat -nCode: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
Zucca wrote:Now I'm left to wonder why bash (--posix) so often complained about bad fd 3 even when I had both ends open...
EDIT2: It doesn't seem to work with busybox sh. However since it worked with bash --posix I call this [solved].
Hmm, I tried BusyBox v1.35.0 (Debian 1:1.35.0-4+b3) built-in shell (ash) and my example seems to work. How do you invoke your commands to get bad fds? How frequently? The last example looks wrong to me.Zucca wrote:I'm starting to think busybox sh fd redirections are broken.
geki wrote:Hmm, I tried BusyBox v1.35.0 (Debian 1:1.35.0-4+b3) built-in shell (ash) and my example seems to work. How do you invoke your commands to get bad fds? How frequently? The last example looks wrong to me.
Code: Select all
$ eshowkw -O busybox
Keywords for sys-apps/busybox:
| | u |
| a a p s l r a | n |
| m r h p p i o i s l m m | e u s | r
| d a m p p c a x a o s 3 p 6 i | a s l | e
| 6 r 6 p p 6 r 8 6 n c 9 h 8 p | p e o | p
| 4 m 4 a c 4 c 6 4 g v 0 a k s | i d t | o
-------------+-------------------------------+-------+-------
[I]1.34.1-r2 | + + + + + + + + ~ ~ ~ ~ ~ ~ ~ | 7 o 0 | gentoo
1.35.0-r2 | o o o o o o o o o o o o o o o | 7 # | gentoo
1.36.1 | ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ | 8 o | gentoo
9999 | o o o o o o o o o o o o o o o | 8 o | gentooCode: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
Code: Select all
$ ( sample 2>&1 1>&3 | cat -n 1>&2 ) 3>&1 | rev
egassem lamron
egassem rorreCode: Select all
$ busybox | head -n 1; qlist -vI busybox
BusyBox v1.36.1 (2023-07-05 23:17:13 -00) multi-call binary.
sys-apps/busybox-1.36.1Code: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!
cat -n gets stdout, rev gets stderr.sh-5.2$ ( echo rofl | cat -n >&2 ) 3>&1 | rev
1 rofl
sh-5.2$ ( echo rofl 2>&1 1>&3 | cat -n >&2 ) 3>&1 | rev
lfor
Code: Select all
init=/sbin/openrc-init
-systemd -logind -elogind seatdI am NaN! I am a man!