使用 tee 重定向文件描述符 3
我几个月前写了这个脚本,现在重读它,我无法解读这行代码的含义:
sudo rsync -xPRSaz --rsync-path='sudo rsync' maeve@macbook:/ macbook/ 3>&1 1>&2 2>&3 | tee macbook.log
我找不到对 sudo
, 文件描述符 3 的任何特殊处理>rsync
或 tee
。重定向之后,我目前猜测情况是这样的:
now fd points to old fd
0 --> 0
1 --> 2
2 --> 1
3 --> 1
- 这些重定向是否应用于 sudo 或 rsync,目的是什么?
- 文件描述符 3 是否未关闭或以任何“不良”方式挂起?
I wrote this script some months ago, and now rereading it, I'm unable to decipher what I meant by this line:
sudo rsync -xPRSaz --rsync-path='sudo rsync' maeve@macbook:/ macbook/ 3>&1 1>&2 2>&3 | tee macbook.log
I can't find any special treatment of file descriptor 3 for sudo
, rsync
or tee
. After the redirects I'm currently guessing this is the situation:
now fd points to old fd
0 --> 0
1 --> 2
2 --> 1
3 --> 1
- Are these redirects applied to
sudo
, or torsync
, and to what end? - Is file descriptor 3 being left unclosed or hanging in any "bad" way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的猜测是对的。交换标准输出和标准错误是一个相当巧妙的技巧。回答您的问题:
sudo
)。sudo
进程本身将检测所有参数并将它们传递给其子命令 (rsync
),但在此之前重定向已被捕获并执行操作:sudo
从未见过他们。Your guess is right. It's a rather nifty trick to swap standard output and standard error. To answer your questions:
sudo
). Thesudo
process itself will detect all the arguments and pass them along to its subcommand (rsync
) but the redirections have been captured and acted upon before that point:sudo
never sees them.请注意,悬空文件描述符
3
可以使用3>&-
关闭,以下是包含的完整行:Note that the dangling file descriptor
3
can be closed with3>&-
, here's the full line with that included: