2016-08-30 2 views
2

Ich schrieb das folgende Shellskript. Ich versuche, Eingaben vom Benutzer mit der getopts-Methode zu erhalten. Dies ist das Getopts-Fragment des Codes, den ich geschrieben habe.Getopts erkennt die angegebenen Optionen nicht als gültig

#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 

Ich habe versucht, das Skript zu starten durch:

./testScript.sh -u myname 

ich die folgende Fehlermeldung erhalten:

illegal option -- u 

Antwort

3
while getopts "$options" opt 
#    ^^  ^

$ Ersatz im Wert der Variablen options. Ohne die $, getopts denkt, die gültigen Flags sind -o, -p, -t, -i, -n und -s.

2

Sie haben vergessen, den Dollar-Zeichen vor der Optionen es eine Variable zu berücksichtigen:

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

Al

Verwandte Themen