Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When passing parameters to getopt, does the order must same as the parsing order?

I want to use getopt to parsing my input, such as –count=123 –range=456 –err

Here is my test code:

argv=$(getopt --name `basename $0` --options '' --longoptions err::,count:,range: -- "$@") 2>&1 || show_usage
eval "set -- ${argv}"
echo "debug> [$argv]"

while true; do
    case "$1" in
        "--count")
            echo "HitCount=$2"
            echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
            shift 2
            echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
            ;;
        "--range")
            echo "ScanRange=$2"
            echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
            shift 2
            echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
            break
            ;;
        "--")
            echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
            shift
            echo "debug> [$1/$2/$3/$4/$5/$6/$7/$8]"
            break
            ;;
        *)
            show_usage
            break
            ;;
    esac
done

test1: it’s good

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

./getopt.sh  --count=1234 --range=5678
debug> [ --count '1234' --range '5678' --]
HitCount=1234
debug> [--count/1234/--range/5678/--///]
debug> [--range/5678/--/////]
ScanRange=5678
debug> [--range/5678/--/////]
debug> [--///////]

test2: I only change the order in command line, but it cannot parsed "–count". I don’t know why?

./getopt.sh --range=5678 --count=1234
debug> [ --range '5678' --count '1234' --]
ScanRange=5678
debug> [--range/5678/--count/1234/--///]
debug> [--count/1234/--/////]

>Solution :

When passing parameters to getopt, does the order must same as the parsing order?

No.

it cannot parsed "–count". I don’t know why?

You have break before "--" so it breaks the loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading