英文:
Shifting positional arguments after call to getopts
问题
Getopts使用OPTIND来存储位置参数的编号。接下来的做法是将位置参数进行如下移动:
shift "$(( OPTIND - 1 ))"
为什么要这样做?
英文:
Getopts uses OPTIND to store the positional argument number. It is then a practice to shift the positional arguments in the following way.
shift "$(( OPTIND - 1 ))"
For what purpose does one do that ?
答案1
得分: 1
使用shift "$(( OPTIND - 1 ))"
来移动位置参数的目的是为了移除已经由getopts处理过的所有选项及其相应的参数。这确保了剩余的参数是非选项参数,可以分别处理。OPTIND
包含下一个由getopts处理的参数的索引,因此从中减去1得到已处理的选项数量,即要移动的参数数量。
英文:
The purpose of shifting the positional arguments using shift "$(( OPTIND - 1 ))"
after using getopts is to remove all the options and their corresponding arguments that have already been processed by getopts. This ensures that the remaining arguments are the non-option arguments that can be processed separately. OPTIND
contains the index of the next argument to be processed by getopts, so subtracting 1 from it gives the number of options processed, which is the number of arguments to shift.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论