Wie kann ich "getopts" ohne Parameter verwenden? (hier für "-v")

301
m1well

Wie kann ich das getoptsohne Parameter verwenden?

while getopts ":a:f:l:r:h:v:" arg; do printf $arg case $arg in a) add_param=$ ;; f) file_param=$ ;; l) list_param=$ ;; r) remove_param=$ ;; v) version_param="version" printf "hello world" ;; h | *) print_usage exit_script esac done 

Ich werde nie in den Fall kommen, -vwenn ich mein Skript so ausführe, sh script.sh -vweil diese Implementierung einen Parameter erwartet.

Kannst du mir helfen, das zu beheben?

0

1 Antwort auf die Frage

1
Kamil Maciorowski

From help getopts (in bash):

getopts: getopts optstring name [arg] Parse option arguments. Getopts is used by shell procedures to parse positional parameters as options. OPTSTRING contains the option letters to be recognized; if a letter is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. 

I think it's valid also for sh. You should try ":a:f:l:r:hv" instead of ":a:f:l:r:h:v:".