getopts не распознает указанные параметры как действительные

Я написал следующий сценарий. Я пытаюсь получить входные данные от пользователя, используя метод getopts. Это фрагмент кода getopts, который я написал.

#Define the help function
function help(){
    echo "Options:";
    echo "-u    Github username"
    echo "-p    Github password"
    echo "-r    Repository name"
    echo "-s    Service name"
    echo "-b    Branch name (Default master)"
    exit 1;
}


#Initialize the default values for the variables.
username="username";
password="password";
rname="rname";
sname="sname";
branch="master";

#Define the getopts variables
options="u:p:r:s:h";

#Start the getopts code
while getopts options opt; do
    case $opt in
            u) #Get the username
                    username=$OPTARG
            ;;
            p) #Get the password
                    password=$OPTARG
            ;;
            r) #Get the repository name
                    rname=$OPTARG
            ;;
            s) #Get the service name
                    sname=$OPTARG
            ;;
            b) #Get the branch name
                    branch=$OPTARG
            ;;
            h) #Execute the help function
        "echo here"
                    help;
            ;;
            \?) #unrecognized option - show help
                    echo "Invalid option."
                    help;
            ;;
    esac
done

#This tells getopts to move on to the next argument.
shift $((OPTIND-1))
#End getopts code

Я попытался запустить скрипт:

./testScript.sh -u myname

Я получаю следующую ошибку:

illegal option -- u

2 ответа

while getopts "$options" opt
#             ^^       ^

$ подставляет в значение переменной options, Без $, getopts считает, что допустимыми флагами являются -o, -p, -t, -i, -n и -s.

Вы забыли знак доллара перед опциями, чтобы считать его переменной:

 #Start the getopts code
 while getopts $options opt; do

Al

Другие вопросы по тегам