Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following works in bash 4, using <A HREF="http://www.gnu.org/software/bash/manual/html_node/Coprocesses.html" rel="nofollow">coprocesses</A>:</p> <pre><code>#!/bin/bash fd_re='^[0-9]+$' cleanup_and_wait() { if [[ ${COPROC[1]} =~ $fd_re ]] ; then eval "exec ${COPROC[1]}&lt;&amp;-" echo "waiting for $filename to finish" &gt;&amp;2 wait $COPROC_PID fi } while IFS= read -r line; do case $line in @*) cleanup_and_wait filename=${line:1} echo "starting $filename" &gt;&amp;2 coproc { sort &gt;"$filename"; echo "Finished with $filename" &gt;&amp;2; } ;; *) printf '%s\n' "$line" &gt;&amp;${COPROC[1]} ;; esac done cleanup_and_wait </code></pre> <p>For prior versions of bash, a named pipe can be used instead:</p> <pre><code>cleanup_and_wait() { if [[ $child_pid ]] ; then exec 4&lt;&amp;- echo "waiting for $filename to finish" &gt;&amp;2 wait $child_pid fi } # this is a bit racy; without a force option to mkfifo, # however, the race is unavoidable fifo_name=$(mktemp -u -t fifo.XXXXXX) if ! mkfifo "$fifo_name" ; then echo "Someone else may have created our temporary FIFO before we did!" &gt;&amp;2 echo "This can indicate an attempt to exploit a race condition as a" &gt;&amp;2 echo "security vulnarability and should always be tested for." &gt;&amp;2 exit 1 fi # ensure that we clean up even on unexpected exits trap 'rm -f "$fifo_name"' EXIT while IFS= read -r line; do case $line in @*) cleanup_and_wait filename=${line:1} echo "starting $filename" &gt;&amp;2 { sort &gt;"$filename"; echo "finished with $filename" &gt;&amp;2; } &lt;"$fifo_name" &amp; child_pid=$! exec 4&gt;"$fifo_name" ;; *) printf '%s\n' "$line" &gt;&amp;4 ;; esac done cleanup_and_wait </code></pre> <p>A few notes:</p> <ul> <li>It's safer to use <code>printf '%s\n' "$line"</code> than <code>echo "$line"</code>; if a line contains only <code>-e</code>, for instance, some versions of <code>echo</code> will do nothing with it.</li> <li>Using an EXIT trap for cleanup ensures that an unexpected SIGTERM or other error won't leave the stale fifo sitting around.</li> <li>If your platform provides a way to create a FIFO with an unknown name in a single, atomic operation, use it; this would avoid the condition that requires us to always test whether the mkfifo is successful.</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload