Re: getopts appears to not be shifting $@ when consuming options

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jan 29, 2021 at 09:36:50PM +0100, Jilles Tjoelker wrote:
> If your actual code is more like:
> 
> set -- -a foo -a bar # for testing
> while getopts :a: arg; do
> ...
> 
> then the script violates the rule I mentioned, and has unspecified
> results.

This, of course, would be the intended use of such a technique.

Having read the a little more of the standard I may have come up with a
solution which both appears to work and without much ugliness.  I hope
it also doesn't violate either the spirit or word of the standard on this
matter:

    #!/bin/sh --

    while getopts :a: arg; do
        case $arg in
            a) set -- "$@" attr="$OPTARG"; shift "$((OPTIND - 1))"; OPTIND=1
        esac
    done
    shift "$((OPTIND - 1))"

It would be quite sad for me to lose this functionality.

P.S. To my amusement this works with bourne as well but shifting needs
     to be undertaken with `expr "$OPTIND" - 1` instead.



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux