неверный индекс массива в операторе if - PullRequest
0 голосов
/ 12 апреля 2020

У меня есть скрипт, в котором я создаю пару аргументов командной строки для передачи (файл и вывод). Сценарий, кажется, работает, так как я могу успешно передать файлы в сценарий и проанализировать их с флагом «-f», но я заметил, что получаю следующую ошибку (в этой строке кода ниже: if [[-n $ {variable [$ arguments_label]}]]):

строка 29: переменные: неверный индекс массива

# declaring a couple of associative arrays
declare -A arguments=();
declare -A variables=();

# declaring an index integer
declare -i index=1;

variables["-o"]="output";
variables["--output"]="output";
variables["-f"]="file";
variables["--file"]="file";

# $@ here represents all arguments passed in
for i in "$@"
do
  arguments[$index]=$i;
  prev_index="$(expr $index - 1)";

  # this if block does something akin to "where $i contains ="
  # "%=*" here strips out everything from the = to the end of the argument leaving only the label
  if [[ $i == *"="* ]]
    then argument_label=${i%=*}
    else argument_label=${arguments[$prev_index]}
  fi

  # this if block only evaluates to true if the argument label exists in the variables array
  if [[ -n ${variables[$argument_label]} ]]
    then
        # dynamically creating variables names using declare
        # "#$argument_label=" here strips out the label leaving only the value
        if [[ $i == *"="* ]]
            then declare ${variables[$argument_label]}=${i#$argument_label=}
            else declare ${variables[$argument_label]}=${arguments[$index]}
        fi
  fi

  index=index+1;
done;

Любые идеи или идеи приветствуются.

...