title: Optional option argument with getopts
body: <pre><code>while getopts &quot;hd:R:&quot; arg; do
    case $arg in
      h)
        echo &quot;usage&quot; 
        ;;
      d)
        dir=$OPTARG
        ;;
      R)
        if [[ $OPTARG =~ ^[0-9]+$ ]];then
          level=$OPTARG
        else
          level=1
        fi
        ;;
      \?)
        echo &quot;WRONG&quot; &gt;&amp;2
        ;;
    esac
  done
  </code></pre>
  <ul>
  <li><p>level refers to the parameter of <code>-R</code>, dir refers to parameters of <code>-d</code></p>
  </li>
  <li><p>when I input  <code>./count.sh -R 1 -d test/</code> it works correctly</p>
  </li>
  <li><p>when I input <code>./count.sh  -d test/ -R 1</code> it works correctly</p>
  </li>
  <li><p>but I want to have it work when I input <code>./count.sh  -d test/ -R</code> or <code>./count.sh  -R -d test/</code></p>
  </li>
  </ul>
  <p>This means that I want <code>-R</code> to have a default value and for the sequence of commands to be more flexible.</p>