From ed6b0fa34884b28399d952cf8edc265cb2f87ce9 Mon Sep 17 00:00:00 2001 From: Paul Morgan <jumanjiman@gmail.com> Date: Fri, 4 Sep 2015 19:22:45 -0400 Subject: [PATCH 1/3] remove unused CLI options * -f is not used in `getopts` * -i is not used in `getopts` * -l needs trailing `:` to mandate `path/to/log/file` * leading `:` is unnecessary in standard shell Signed-off-by: Paul Morgan <jumanjiman@gmail.com> --- docker-bench-security.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-bench-security.sh b/docker-bench-security.sh index 7a2b512..6442e0e 100755 --- a/docker-bench-security.sh +++ b/docker-bench-security.sh @@ -62,7 +62,7 @@ if [ "x$ID" != "x0" ]; then fi # Get the flags -while getopts :hlfi: args +while getopts hl: args do case $args in h) usage ;; From aaffcb8df122a8ddfe2b282197bdd44c95f5bd22 Mon Sep 17 00:00:00 2001 From: Paul Morgan <jumanjiman@gmail.com> Date: Fri, 4 Sep 2015 19:25:13 -0400 Subject: [PATCH 2/3] show logging option when user asks for help * avoid printf to simplify usage() syntax * add reminder to update usage() when CLI option is added * preserve indentation for help output Signed-off-by: Paul Morgan <jumanjiman@gmail.com> --- docker-bench-security.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/docker-bench-security.sh b/docker-bench-security.sh index 6442e0e..6547c23 100755 --- a/docker-bench-security.sh +++ b/docker-bench-security.sh @@ -35,10 +35,12 @@ if [ $? -ne 0 ]; then fi usage () { - printf " - usage: %s [options] + cat <<EOF + usage: ${myname} [options] - -h optional Print this help message\n" "$myname" + -h optional Print this help message + -l PATH optional Log output in PATH +EOF exit 1 } @@ -62,6 +64,8 @@ if [ "x$ID" != "x0" ]; then fi # Get the flags +# If you add an option here, please +# remember to update usage() above. while getopts hl: args do case $args in From 085b260a7b490c074f3c6dee846c60f83459a0ea Mon Sep 17 00:00:00 2001 From: Paul Morgan <jumanjiman@gmail.com> Date: Fri, 4 Sep 2015 19:34:00 -0400 Subject: [PATCH 3/3] exit with proper status on CLI options * if `-h` is used: exit good * if a non-valid CLI option is used without `-h`: exit bad Signed-off-by: Paul Morgan <jumanjiman@gmail.com> --- docker-bench-security.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/docker-bench-security.sh b/docker-bench-security.sh index 6547c23..fd1be3f 100755 --- a/docker-bench-security.sh +++ b/docker-bench-security.sh @@ -41,7 +41,6 @@ usage () { -h optional Print this help message -l PATH optional Log output in PATH EOF - exit 1 } yell "# ------------------------------------------------------------------------------ @@ -69,9 +68,9 @@ fi while getopts hl: args do case $args in - h) usage ;; + h) usage; exit 0 ;; l) logger="$OPTARG" ;; - *) usage ;; + *) usage; exit 1 ;; esac done