<h2 id="introduction">Introduction</h2>
<p>I am in the process of rewriting some scripts I use to manage <a href="https://magnatune.com/" title="Magnatune">Magnatune</a> albums. I’m a lifetime Magnatune member and have access to the whole music collection. I wrote a script for downloading albums and placing them in my <code>~/Music</code> directory which I talked about in 2013 (show <a href="https://hackerpublicradio.org/eps.php?id=1204">1204</a>). The original scripts are still available on <a href="https://gitlab.com/davmo/magnatune-downloader">GitLab</a> and I know of one other person who made use of them!</p>
<p>Since 2013 I have written a few other support scripts, for example one to manage a queue of albums I want to buy and download, and one which summarises the state of this queue.</p>
<p>It’s this <code>'show_queue'</code> script I am currently updating (called <a href="https://hackerpublicradio.org/eps/hpr3413/show_queue_orig"><code>show_queue_orig</code></a>, and available in the resources to this show). The original version of this script took Magnatune album URLs from a file (acting as a queue of stuff I wanted to buy), parsed out a piece of the URL and used it to <code>grep</code> a pre-prepared summary in another file. This file of summaries had been made from a master XML file provided by Magnatune (see <code>update_albums</code> on GitLab).</p>
<p>Magnatune has moved away from this master XML file to a SQLite database in recent years, so I want to perform a database lookup for each URL to list its details.</p>
<p>The first version of the new script wasn’t difficult to write: just extract the search data as before and run a query on the database using this data. I have included this script which I call <a href="https://hackerpublicradio.org/eps/hpr3413/show_queue_db_1"><code>show_queue_db_1</code></a> amongst the resources for this episode, so you can see what I’m talking about – and what I want to improve on. It felt bad to be performing multiple calls on the <code>sqlite3</code> command in a loop, so I looked around for an alternative way.</p>
<p>In April 2019 <a href="https://hackerpublicradio.org/correspondents/0311.html">clacke</a> did a show (number <a href="https://hackerpublicradio.org/eps.php?id=2793">2793</a>) about the Bash <code>coproc</code> command.</p>
<p>This command creates a <em>subshell</em> running a command or group of commands which is connected to the calling (<em>parent</em>) process through two file descriptors (<em>FD</em>s). It’s possible for the calling shell to write to the input descriptor and read from the output one and thereby communicate with whatever is running in the subshell.</p>
<p>I was vaguely aware of <code>coproc</code> at the time of clacke’s show but hadn’t looked into it. I found the show fascinating but didn’t have a use for the feature at the time.</p>
<p>To solve my need to show my Magnatune queue of future purchases, it looked as if a <code>sqlite3</code> instance running in a subshell could be given queries one after the other and return the answers I needed. My journey to a Bash script using <code>coproc</code> then followed.</p>
<h2 id="long-notes">Long notes</h2>
<p>Follow this link to <a href="https://hackerpublicradio.org/eps/hpr3413/full_shownotes.html">read the detailed notes</a> associated with this episode.</p>
<h2 id="links">Links</h2>
<ul>
<li>Bash Reference Manual:
<ul>
<li><a href="https://www.gnu.org/software/bash/manual/bash.html#Coprocesses">3.2.6 Coprocesses</a></li>
<li><a href="https://www.gnu.org/software/bash/manual/bash.html#Duplicating-File-Descriptors">3.6.8 Duplicating File Descriptors</a></li>
</ul></li>
</ul>
<ul>
<li>Stack Exchange:
<ul>
<li><a href="https://unix.stackexchange.com/questions/86270/how-do-you-use-the-command-coproc-in-various-shells">How do you use the command coproc in various shells?</a></li>
</ul></li>
</ul>
<ul>
<li>Wikipedia:
<ul>
<li><a href="https://en.wikipedia.org/wiki/Expect">Wikipedia page on <code>expect</code></a></li>
</ul></li>
</ul>
<ul>
<li>Other links:
<ul>
<li><a href="https://hackerpublicradio.org/eps.php?id=1204">hpr1204 :: My Magnatune Downloader</a></li>
<li><a href="https://gitlab.com/davmo/magnatune-downloader">GitLab repository: original magnatune-downloader</a></li>
<li><a href="https://hackerpublicradio.org/eps.php?id=2793">hpr2793 :: bash coproc: the future (2009) is here</a></li>
</ul></li>
</ul>
<ul>
<li>Resources:
<ul>
<li>Example files:
<ul>
<li><a href="https://hackerpublicradio.org/eps/hpr3413/coproc_test.sh">coproc_test.sh</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3413/coproc_test.awk">coproc_test.awk</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3413/show_queue_orig">show_queue_orig</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3413/show_queue_db_1">show_queue_db_1</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3413/show_queue_db_2">show_queue_db_2</a></li>
<li><a href="https://hackerpublicradio.org/eps/hpr3413/show_queue.pl.zip">show_queue.pl.zip</a></li>
</ul></li>
</ul></li>
</ul>
