bash - What is a reason for using shift $((OPTIND-1)) after getopts? -


I realize that the shift moves the array of cli args n spaces from the left, and the default of n is 1. This means that I can provide value to the current variables array while using $ 1 while inside a loop. Why I do not understand why it is used in this context below is the input algorithms already assigned to the values ​​and removing the change $ (OPTIND-1) does not change this fact. Source: When Receiving ": h: a: fc" opt. What to do $ opt in h) print_help exit 0 ;; A) Aha = $ {OPTARG} ;; F) force = 1 ;; C) Clean = 1 ;; \?) Echoerr "invalid option - $ OPTARG" print_help exit 1 ;; ASAC changes $ ((OPTIND-1))

shift removes processed parameters getopts by loop from the parameter list, so that the rest of the script command line (if any) with a balance of $ 1 ... process normally, without worrying for the number of Consider a hypothetical script with the use of getopts of options

  frobble [-f] [-c] [-a hostname] file Name ...   

takes care of parsing getopts loop -f , -c and -a , if they exist, but do not remove them from the logic list. This means that to get your file name logic, you have to figure out how many options were processed, and continue processing from there. Convenient, getopts first tells you the paused argument: the variable OPTIND .

And instead of messing with offsets and stuff, you simply discard processed options, re-enter the remaining arguments so that your filename is always $ 1 .

This is what shift $ (OPTIND-1)) does.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -