Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am (like everyone else, it seems) a bit confused about exactly what the goal is here, so I'll give three possible answers that may cover what you actually want. First, the relatively simple goal of getting the script to read from either a list of files (supplied on the command line) or from its regular stdin:</p> <pre><code>if [ $# -gt 0 ]; then exec &lt; &lt;(cat "$@") fi # From this point on, the script's stdin is redirected from the files # (if any) supplied on the command line </code></pre> <p>Note: the double-quoted use of $@ is the best way to avoid problems with funny characters (e.g. spaces) in filenames -- $* and unquoted $@ both mess this up. The &lt;() trick I'm using here is a bash-only feature; it fires off cat in the background to feed data from files supplied on the command line, and then we use exec to replace the script's stdin with the output from cat.</p> <p>...but that doesn't seem to be what you actually want. What you seem to really want is to pass the supplied filenames or the script's stdin <em>as arguments</em> to a command inside the script. This requires sort of the opposite process: converting the script's stdin into a file (actually a named pipe) whose name can be passed to the command. Like this:</p> <pre><code>if [[ $# -gt 0 ]]; then command "$@" &lt;&lt;EOF here-doc goes here EOF else command &lt;(cat) &lt;&lt;EOF here-doc goes here EOF fi </code></pre> <p>This uses &lt;() to launder the script's stdin through cat to a named pipe, which is then passed to command as an argument. Meanwhile, command's stdin is taken from the here-doc.</p> <p>Now, I think that's what you want to do, but it's not quite what you've asked for, which is to <em>both</em> redirect the script's stdin from the supplied files <em>and</em> pass stdin to the command inside the script. This can be done by combining the above techniques:</p> <pre><code>if [ $# -gt 0 ]; then exec &lt; &lt;(cat "$@") fi command &lt;(cat) &lt;&lt;EOF here-doc goes here EOF </code></pre> <p>...although I can't think why you'd actually want to do this.</p>
 

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