Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>$@</code> variable, when quoted will be able to group parameters as they should be:</p> <pre><code>for parameter in "$@" do echo "The parameter is '$parameter'" done </code></pre> <p>If given:</p> <pre><code>head -100 test this "File name" out </code></pre> <p>Will print</p> <pre><code>the parameter is 'head' the parameter is '-100' the parameter is 'test' the parameter is 'this' the parameter is 'File name' the parameter is 'out' </code></pre> <p>Now, all you have to do is parse the loop out. You can use some very simple rules:</p> <ol> <li>The first parameter is always the file name</li> <li>The parameters that follow that start with a dash are parameters</li> <li>After the "--" or once one doesn't start with a "-", the rest are all file names.</li> </ol> <p>You can check to see if the first character in the parameter is a dash by using this:</p> <pre><code>if [[ "x${parameter}" == "x${parameter#-}" ]] </code></pre> <p>If you haven't seen this syntax before, it's a <em>left filter</em>. The <code>#</code> divides the two parts of the variable name. The first part is the name of the variable, and the second is the <strong>glob</strong> filter (not regular expression) to cut off. In this case, it's a single dash. As long as this statement isn't true, you know you have a parameter. BTW, the <code>x</code> may or may not be needed in this case. When you run a test, and you have a string with a dash in it, the test might mistake it for a parameter of the test and not the value.</p> <p>Put it together would be something like this:</p> <pre><code>parameterFlag="" for parameter in "$@" #Quotes are important! do if [[ "x${parameter}" == "x${parameter#-}" ]] then parameterFlag="Tripped!" fi if [[ "x${parameter}" == "x--" ]] then print "Parameter \"$parameter\" ends the parameter list" parameterFlag="TRIPPED!" fi if [ -n $parameterFlag ] then print "\"$parameter\" is a file" else echo "The parameter \"$parameter\" is a parameter" fi done </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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.
    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