bash 用 tee 进行 IO 重定向,没有复杂的语法,它是如何工作的?

bash 用 tee 进行 IO 重定向,没有复杂的语法,它是如何工作的?

一种方法隐含的 dup(2)输出在fd {10,11,12}一个真实用例并在之后立即关闭它,基于在此:

$ cat tags
desktop-19.9.0
foobar-1.2.3
desktop-22.9.0
mobile-24.10.0
desktop-18.9.0
desktop-21.9.0
mobile-23.10.0
foobar-1.2.4
desktop-17.8.0
desktop-20.8.0
mobile-22.9.0
desktop-16.8.0
desktop-19.8.0
mobile-21.9.0
foobar-1.2.5
desktop-15.7.0
desktop-18.7.0
mobile-20.8.0
desktop-14.7.0
desktop-17.7.0
mobile-19.8.0
desktop-13.6.0
desktop-16.6.0
mobile-18.7.0
foobar-1.2.6
desktop-12.6.0
desktop-15.6.0
mobile-17.7.0
desktop-11.5.0
desktop-14.5.0
mobile-16.6.0
desktop-10.5.0
desktop-13.5.0
mobile-15.6.0
desktop-9.4.0
desktop-12.4.0
mobile-14.5.0
desktop-8.4.0
desktop-11.4.0
mobile-13.5.0
desktop-7.3.0
foobar-1.2.7
desktop-10.3.0
mobile-12.4.0
desktop-6.3.0
desktop-9.3.0
mobile-11.4.0
desktop-5.2.0
desktop-8.2.0
mobile-10.3.0
desktop-4.2.0
desktop-7.2.0
mobile-9.3.0
desktop-3.1.0
desktop-6.1.0
mobile-8.2.0
desktop-2.1.0
desktop-5.1.0
mobile-7.2.0
desktop-1.0.0
foobar-1.2.8
desktop-4.0.0
mobile-6.1.0
cat tags        | tee  /dev/fd/{10,11,12} 10> >(
grep -w desktop | tail -n 3) 11> >(
grep -w mobile  | tail -n 3) 12> >(
grep -w foobar  | tail -n 3) 1>/dev/null

输出:

foobar-1.2.6
foobar-1.2.7
foobar-1.2.8
mobile-8.2.0
mobile-7.2.0
mobile-6.1.0
desktop-5.1.0
desktop-1.0.0
desktop-4.0.0

这是有效的,但我没有解释怎么运行的。幕后发生了什么?

为什么fd自动关闭我不清楚。可以使用显式的 open/close 来编写吗fd

test -e /dev/fd/10 && echo open || echo close
close

编辑:

可以简化为:

cat tags         | tee >(
grep -w mobile   | tail -n3) >(
grep -w desktop  | tail -n3) >(
grep -w foobar   | tail -n3) > /dev/null

调试系统调用:

$ strace -fff -o log bash -c '
cat tags         | tee >(
grep -w mobile   | tail -n3) >(
grep -w desktop  | tail -n3) >(
grep -w foobar   | tail -n3) > /dev/null'

$ less log.*

$ grep dup log*
log.2476802:dup2(4, 1)                              = 1
log.2476803:dup2(3, 0)                              = 0
log.2476803:dup2(4, 63)                             = 63
log.2476803:dup2(4, 62)                             = 62
log.2476803:dup2(4, 61)                             = 61
log.2476803:dup2(3, 1)                              = 1
log.2476804:dup2(3, 0)                              = 0
log.2476805:dup2(4, 1)                              = 1
log.2476806:dup2(3, 0)                              = 0
log.2476807:dup2(3, 0)                              = 0
log.2476808:dup2(4, 1)                              = 1
log.2476809:dup2(3, 0)                              = 0
log.2476810:dup2(3, 0)                              = 0
log.2476811:dup2(4, 1)                              = 1
log.2476812:dup2(3, 0)                              = 0

$ LANG=C grep close log.*
log.2476801:close(3)                                = 0
log.2476801:close(4)                                = 0
log.2476801:close(4)                                = -1 EBADF (Mauvais descripteur de fichier)
log.2476801:close(3)                                = -1 EBADF (Mauvais descripteur de fichier)
log.2476802:close(4)                                = 0
log.2476802:close(3)                                = 0
log.2476802:close(1)                                = 0
log.2476802:close(2)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(4)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(4)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(4)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(3)                                = 0
log.2476803:close(4)                                = 0
log.2476803:close(5)                                = 0
log.2476803:close(0)                                = 0
log.2476804:close(63)                               = 0
log.2476804:close(4)                                = -1 EBADF (Mauvais descripteur de fichier)
log.2476804:close(3)                                = 0
log.2476804:close(3)                                = -1 EBADF (Mauvais descripteur de fichier)
[...]

有关的:http://mywiki.wooledge.org/ProcessSubstitution

答案1

当你写的时候tee /dev/fd/{10,11,12} 10> (…) 11> (…) 12> (…)

shell 首先创建管道,创建子进程(继承管道),关闭未使用的管道末端并重新映射文​​件目录。然后当命令执行时,/dev/fd/10(或11、或12)就可以打开了。

内核只显示可用的文件描述符。重定向10>的范围仅限于其命令。

如果你想strace向你展示dup2,你最好尝试strace -ff bash -c "cmd1 >(cmd2)" 2>&1 | grep dup举例。

相关内容