What's new

Help on busybox getopt usage in asuswrt?

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

vibroverbus

Regular Contributor
Long story short, trying to port a shell script that uses getopts. No getopts on asuswrt, but we have getopt in busybox. Well, as often with busybox implementation.... the doc blows. I'm a pretty good googleborg user and have come up with tons of stuff including a number of of getopts vs getopt posts (several of which are horrible in their own way), but much doesn't seem to apply or work.

So - some getopt examples seem to suggest you can recursively call getopt to load args into a variable until the last arg has been hit - like getopts - but AFAICT that doesn't work at least in the BB implementation...

Is that right? Do you just call getopt once, then just use shift to churn through $1 & $2 ? I found an example or two showing that, as well as a number showing you can use getopt to load a user defined variable, or, OPTS & OPTARG (as getopts does), so its all clear as mud...
 
Code:
admin@RT-AC68U:/# getopt -?
getopt: invalid option -- ?
BusyBox v1.25.1 (2018-01-26 08:53:58 MST) multi-call binary.

Usage: getopt [OPTIONS] [--] OPTSTRING PARAMS

        -a,--alternative                Allow long options starting with single -
        -l,--longoptions=LOPT[,...]     Long options to recognize
        -n,--name=PROGNAME              The name under which errors are reported
        -o,--options=OPTSTRING          Short options to recognize
        -q,--quiet                      No error messages on unrecognized options
        -Q,--quiet-output               No normal output
        -s,--shell=SHELL                Set shell quoting conventions
        -T,--test                       Version test (exits with 4)
        -u,--unquoted                   Don't quote output

Example:

O=`getopt -l bb: -- ab:c:: "$@"` || exit 1
eval set -- "$O"
while true; do
        case "$1" in
        -a)     echo A; shift;;
        -b|--bb) echo "B:'$2'"; shift 2;;
        -c)     case "$2" in
                "")     echo C; shift 2;;
                *)      echo "C:'$2'"; shift 2;;
                esac;;
        --)     shift; break;;
        *)      echo Error; exit 1;;
        esac
done
admin@RT-AC68U:/#
 
yeah... that's what I came across and what I mean by crap doc. seems pretty pointless implementation, for all the work you have to do, might as well parse strings yourself... oh well... unfortunately I'm trying to get this script to run on both DD-WRT and asuswrt and DD-WRT has getopts and works fine with that... but asuswrt only has getopt...
 

Similar threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top