File descriptor 3 redirection with tee

I wrote this script a few months ago, and now re-reading it, I can not decrypt 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 cannot find any special file descriptor 3 handling for sudo , rsync or tee . After the redirect, which I am guessing right now, this is the situation:

 now fd points to old fd 0 --> 0 1 --> 2 2 --> 1 3 --> 1 
  • Do these redirects apply to sudo or rsync and to which end?
  • Is file descriptor 3 open or dangling in any "bad" way?
+4
source share
2 answers

Your guess is correct. This is a pretty neat trick to replace standard output and standard error. To answer your questions:

  • these redirects are captured by the shell, so they apply to that part of the pipeline (which is sudo ). The sudo process itself detects all the arguments and passes them to its sub-command ( rsync ), but the redirects were captured and acted up to this point: sudo never sees them.
  • File descriptor 3 does not remain hanging. It will be closed when the process is completed.
+1
source

Note that the dangling file descriptor 3 can be closed with 3>&- , here is the full line with it turned on:

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

Source: https://habr.com/ru/post/1339932/


All Articles