getopts в функции, которая вызывается более одного раза в скрипт, getopts не обнаруживает никаких опций после 1-го вызова функции [дубликат]

Además de la respuesta de Jan Marek... He notado que puedes salirte con la tuya:

ls --full or ls --fu

que hará lo mismo que ls --full-timecomo él describió. Gracias Stéphane Chazelas. Ahora escribo ls --fuen todas partes.:)

1
15.06.2019, 07:41
1 ответ

Каждый раз, когда вы вызываете getopts , он использует $OPTIND;

If the application sets OPTIND to the value 1, a new set of parameters can be used: either the current positional parameters or new arg values. Any other attempt to invoke getopts multiple times in a single shell execution environment with parameters (positional parameters or arg operands) that are not the same in all invocations, or with an OPTIND value modified to be a value other than 1, produces unspecified results.

(курсив мой ). Вам нужно каждый раз сбрасывать OPTIND перед вызовом getopts, возможно здесь:

#...
  # parse flags
  OPTIND=1
  while getopts 't:s:e:h' flag; do
#...
1
27.01.2020, 23:41

Теги

Похожие вопросы