no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | getopts [2014/04/01 17:56] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== getopts ===== | ||
+ | * [[bash]] | ||
+ | * [[getopt]] | ||
+ | |||
+ | * [[http:// | ||
+ | |||
+ | '' | ||
+ | |||
+ | ==== getopts Syntax ==== | ||
+ | |||
+ | <code bash> | ||
+ | getopts optstring name [args] | ||
+ | </ | ||
+ | |||
+ | ==== Setting options ==== | ||
+ | |||
+ | Use the '' | ||
+ | |||
+ | Here's a sample that accepts '' | ||
+ | |||
+ | Strings for passed arguments must occur after a whitespace. | ||
+ | |||
+ | <code bash> | ||
+ | getopts abc opts | ||
+ | </ | ||
+ | |||
+ | To set an argument as **required**, | ||
+ | |||
+ | <code bash> | ||
+ | getopts ab:c | ||
+ | </ | ||
+ | |||
+ | ==== Invalid Options ==== | ||
+ | |||
+ | If an invalid option is passed, getopts will throw an error by default: | ||
+ | |||
+ | < | ||
+ | sample.sh -d | ||
+ | sample.sh: illegal option -- d | ||
+ | </ | ||
+ | |||
+ | When an invalid option is passed, the '' | ||
+ | |||
+ | <code bash> | ||
+ | case ?) | ||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | There are two ways to silence errors from bash. One is to set '' | ||
+ | |||
+ | Valid syntax for both: | ||
+ | |||
+ | < | ||
+ | OPTERR=0 | ||
+ | opts=:abc | ||
+ | </ | ||
+ | |||
+ | Best practice would be to silence the errors using '': | ||
+ | |||
+ | <code bash> | ||
+ | case ?) | ||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== Option Arguments ==== | ||
+ | |||
+ | Arguments following options must have a whitespace between them. | ||
+ | |||
+ | To toggle an option as requiring an argument, add a '':'' | ||
+ | |||
+ | Require '' | ||
+ | |||
+ | <code bash> | ||
+ | getopts abc: opt | ||
+ | </ | ||
+ | |||
+ | The argument passed to the option is set as '' | ||
+ | |||
+ | <code bash> | ||
+ | c) | ||
+ | echo " | ||
+ | </ | ||
+ | |||
+ | === Multiple Arguments === | ||
+ | |||
+ | There are two ways to pass multiple arguments to an option: enclose the argument in quotes, or pass the option more than once and build on the old variable. | ||
+ | |||
+ | ** Quoted argument ** | ||
+ | |||
+ | <code bash> | ||
+ | bkup.mysql -d "mysql client" | ||
+ | ... | ||
+ | d) | ||
+ | DBS=$OPTARG | ||
+ | </ | ||
+ | |||
+ | ** Multiple options ** | ||
+ | |||
+ | <code bash> | ||
+ | bkup.mysql -d mysql -d client | ||
+ | ... | ||
+ | d) | ||
+ | DBS=" | ||
+ | </ | ||
+ | |||
+ | ==== Missing Argument ==== | ||
+ | |||
+ | If error reporting is silenced using '':'' | ||
+ | |||
+ | <code bash> | ||
+ | bkup.mysql -d | ||
+ | ... | ||
+ | getopts abcd: | ||
+ | ... | ||
+ | :) | ||
+ | echo " | ||
+ | </ | ||
+ | ==== Example ==== | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | |||
+ | while getopts ": | ||
+ | case $opt in | ||
+ | a) | ||
+ | echo "-a was triggered, Parameter: $OPTARG" | ||
+ | ;; | ||
+ | \?) | ||
+ | echo " | ||
+ | exit 1 | ||
+ | ;; | ||
+ | :) | ||
+ | echo " | ||
+ | exit 1 | ||
+ | ;; | ||
+ | esac | ||
+ | done | ||
+ | </ |