Bash getopts function that destroys command line options?

Can you use the bash "getopts" function twice in the same script?

I have a set of parameters that will mean different things depending on the value of a particular parameter. Since I cannot guarantee that getopts will evaluate this particular parameter first, I would like to run getopts once using only that particular parameter, and then run it a second time using other parameters.

+3
source share
2 answers

Yes, just reset OPTIND afterwards.

#!/bin/bash

set -- -1
while getopts 1 opt; do
    case "${opt}" in
        1) echo "Worked!";;
        *) exit 1;
    esac
done

OPTIND=1
set -- -2
while getopts 2 opt; do
    case "${opt}" in
        2) echo "Worked!";;
        *) exit 1;
    esac
done
+4
source

getopts , getopt. getopts bash , .

. bash.

.

,

Rob

0

Source: https://habr.com/ru/post/1697618/


All Articles