fnisi
fnisi

Reputation: 1223

Shell redirection - dialog

I cannot interpret the code blow

exec 3>&1
INTERFACE=`echo $DIALOG_TAGS | xargs dialog --title 'Test' --menu ' Text comes here :' 0 0 0 2>&1 1>&3`
exec 3>&-
: > $TMPDIR/._tmp.conf

My question is; FD#3 is created with exec 3>&1 and redirected to where FD#1 goes. In the second line, we have 2>&1 1>&3 which confuses me.

Another part is the last line, where : and a > redirection take place.

Any explanations will be highly appreciated! Thanks

Upvotes: 1

Views: 369

Answers (1)

DevSolar
DevSolar

Reputation: 70293

exec 3>&1

FD #3 is created, writing to FD #1 (stdout).

INTERFACE=`echo $DIALOG_TAGS | xargs dialog --title 'Test' --menu ' Text comes here :' 0 0 0 2>&1 1>&3`

The command in backtics is executed. (In this case, a dialog menu displaying the tag / item pairs defined in DIALOG_TAGS.)

The FD #2 (stderr) is redirected to FD #1 (stdout, which gets assigned to INTERFACE due to the backticks). The FD #1 (stdout) is redirected to FD #3.

The idea is as follows:

  • dialog displays a menu from which you can select an item. The menu, obviously, needs stdout to write to. The result (selection) from the menu is, by default, written to stderr.
  • The author wants to capture the result, thus he redirects stderr to &1 so the result ends up in INTERFACE.
  • However he wants only the result, not the whole menu... so instead of capturing the menu and the result, he redirects the menu (stdout) to the previously-created copy of stdout, leaving only the result to be sent to FD #1, and thus stored in INTERFACE, while the menu is being displayed via the FD #3 (which is still pointing to the terminal, regardless of the backticks).

Once that is done...

exec 3>&-

FD #3 is closed, as it is no longer needed.


: > $TMPDIR/._tmp.conf

This, I couldn't understand. The colon is a builtin command that does nothing, except parameter expansion and redirection. It creates an empty file...? Perhaps to indicate to some other program that the selection is made...? Or to preserve the time stamp...? No idea.

Upvotes: 3

Related Questions