Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The last item in your <code>case</code> statement should have a backslash instead of a slash. Let's make a couple of other changes for the sake of diagnostics so you can see what's happening.</p> <pre><code>#!/bin/ksh wflag=false cflag=false # the first colon means suppress error messages # the colon after the x means that -x requires an argument while getopts ":cwx:" opt do echo "opt $opt" case "$opt" in c) cflag=true ;; w) wflag=true ;; x) echo "x is an option, $OPTARG is its argument" ;; \?) echo "invalid option" ;; *) echo "missing argument" ;; esac done echo "cflag $cflag" echo "wflag $wflag" </code></pre> <p>Now let's call it several ways:</p> <pre><code>$ ./script.ksh -c opt [c] cflag true wflag false </code></pre> <p>Fine.</p> <pre><code>$ ./getoptstest.ksh -c -w opt [c] opt [w] cflag true wflag true </code></pre> <p>Also fine.</p> <pre><code>$ ./getoptstest.ksh -cw opt [c] opt [w] cflag true wflag true </code></pre> <p>Still fine, but our answer is foreshadowed.</p> <pre><code>$ ./getoptstest.ksh '-c -w' opt [c] opt [?] invalid option opt [?] invalid option opt [w] cflag true wflag true </code></pre> <p>Now we're getting somewhere. The Korn shell <code>man</code> page says:</p> <blockquote> <p>A leading : in optstring causes getopts to store the letter of an invalid option in OPTARG, and to <strong>set vname to ? for an unknown option</strong> and to : when a required option argument is miss‐ ing. Otherwise, getopts prints an error message.</p> </blockquote> <p>As you can see from the example before this last one, you can run the options together as in <code>-cw</code>. When you pass an option like <code>'-c -w'</code>, <code>getopts</code> sees it as <code>-c</code> <code>-space</code> <code>--</code> and <code>-w</code>. So, in addition to two invalid options (<code>--</code> and <code>-space</code>) it sees both <code>-c</code> and <code>-w</code> and that's why <strong><em>both</em></strong> flags are getting set. Without the leading colon (which is the way the original script is posted in the question), you should be getting error messages like these:</p> <pre><code>./script.ksh: - : unknown option ./script.ksh: --: unknown option </code></pre> <p>If you look closely, you'll notice the telltale space and extra dash.</p> <p>It's unfortunate that the <code>man</code> page doesn't discuss the ability of <code>getopts</code> to process arguments passed separately or bunched together.</p> <p>I'm not sure if I understand what or if there's a problem with your <code>ksh program.ksh "what up"</code> example.</p> <p>Try my modified script with these options and arguments to see how the rest of it works.</p> <pre><code>./script.ksh -x ./script.ksh -x foo ./script.ksh -z ./script.ksh -c bar -w </code></pre> <p>That last one is tricky. When an argument that doesn't begin with a dash is found and the preceding option doesn't take an argument, the <code>getopts</code> stops processing and the rest of the arguments are left in place. You can use <code>shift $(($OPTIND - 1))</code> after the <code>while</code> loop and then you'll be able to access the remaining positional parameters in the usual way.</p> <p>Unfortunately, the shell builtin <code>getopts</code> doesn't handle long options like <code>--version</code>. The external utility <code>getopt</code> does, but there can be some issues that have to be dealt with when it is used.</p>
    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