开发者

Redirecting file descriptor 3 with tee

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 p开发者_JS百科oints to old fd
     0    -->         0
     1    -->         2
     2    -->         1
     3    -->         1
  • Are these redirects applied to sudo, or to rsync, and to what end?
  • Is file descriptor 3 being left unclosed or hanging in any "bad" way?


Your guess is right. It's a rather nifty trick to swap standard output and standard error. To answer your questions:

  • these redirections are captured by the shell so they apply to that portion of the pipeline (which is sudo). The sudo 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.
  • File handle 3 is not left hanging. It will be closed when the process ends.


Note that the dangling file descriptor 3 can be closed with 3>&-, here's the full line with that included:

sudo rsync -xPRSaz --rsync-path='sudo rsync' maeve@macbook:/ macbook/ 3>&1 1>&2 \
2>&3 3>&- | tee macbook.log
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜