getopts won't call twice in a row? [duplicate]

淺唱寂寞╮ 提交于 2019-12-04 02:13:00

Just add:

local OPTIND

at the top of your function.

To explain why Dennis's answer works, see the bash man page (search for getopts):

OPTIND is initialized to 1 each time the shell or a shell script is invoked.

The shell does not reset OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.

This is how getopts can process multiple options.

If getopts didn't maintain global state in the OPTIND variable, each call to getopts in your while loop would keep processing $1, and never advance to the next argument.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!