<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Aptos;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Aptos",sans-serif;
        mso-ligatures:standardcontextual;
        mso-fareast-language:EN-US;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Aptos",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:11.0pt;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-CA" link="#467886" vlink="#96607D" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">I’m trying to debug a shell script that uses dialog(1), which has the unfortunate habit of overwriting the debug output I wanted to see.<o:p></o:p></p>
<p class="MsoNormal">After quite some time banging my head against the wall, I grabbed the source to dialog to look at their examples.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Their examples follow a pattern I have NOT been following; I’m now looking at this trying to understand WTF it does:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="text-indent:.5in">1             exec 3>&1<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">2             returntext=`$DIALOG --title "RANGE BOX" --rangebox "Please set the volume..." 0 60 0 123 5 2>&1 1>&3`<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">3             returncode=$?<o:p></o:p></p>
<p class="MsoNormal" style="text-indent:.5in">4             exec 3>&-<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">so… ok.  <o:p></o:p></p>
<p class="MsoNormal">Line 1 duplicates FD 1 (stdout) into FD 3.  That’s usually so we can mess with FD 1 later on and still have a handle to stdout.  Good so far.<o:p></o:p></p>
<p class="MsoNormal">Line 2 runs /usr/bin/dialog in a subshell, the options to dialog(1) are irrelevant here.  I’m looking at the redirects. 
<o:p></o:p></p>
<p class="MsoNormal">    “2>&1” duplicates FD 1 into FD 2, effectively throwing away the handle to the inherited /dev/stderr and forcing stderr output onto stdout.  That’s a good thing because dialog(1) reports its results onto stderr by default to avoid messing
 with curses(3) output.<o:p></o:p></p>
<p class="MsoNormal">    “1>&3” however, throws away stdout (FD 1), replacing it with FD 3 which is… already a copy of FD 1 ???<o:p></o:p></p>
<p class="MsoNormal">Line 3 is obvious.<o:p></o:p></p>
<p class="MsoNormal">Line 4 closes FD 3.  Not sure that we need to bother, but OK, cleaning up behind ourselves is usually a good thing.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I get the need for swapping stdin/stderr, as dialog(1) – well, really curses(3) - uses stdout to paint the screen, so dialog(1) spits its results out on stderr to avoid conflicting.  If we’re in a subshell trying to capture that output,
 we need to grab it just as dialog(1) exits – and $() assignment only captures stdout.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">But what’s the point of having FD 3 in this setup, if all we do is assign it to stdin inside the subshell and then forget about it?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Can anyone see what I’m missing here?<o:p></o:p></p>
<p class="MsoNormal">-Adam<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>