-
-
Save SaurabhG20/c3d482f3f077993359a3b98075bfcc24 to your computer and use it in GitHub Desktop.
Revisions
-
Jonathan D Kelley revised this gist
Dec 13, 2019 . 1 changed file with 0 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,8 +6,6 @@ TEST_MODE=FALSE ENDPOINT_HOSTNAME="" AUX="" FLAG_LIST=( "test" -
Jonathan D Kelley created this gist
Dec 13, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ # Bash getopt long options with values usage example ```sh #!/bin/bash -e # default args TEST_MODE=FALSE ENDPOINT_HOSTNAME="" AUX="" # getopt args options=$(getopt -o THPSA: --long test,host,check_prefix,check_suffix,handlers: --name "$0" -- "$@") FLAG_LIST=( "test" ) ARGUMENT_LIST=( "endpoint" "argaux" ) # read arguments opts=$(getopt \ --longoptions "$(printf "%s," "${FLAG_LIST[@]}"),$(printf "%s:," "${ARGUMENT_LIST[@]}")" \ --name "$(basename "$0")" \ --options "" \ -- "$@" ) eval set --$opts while [[ $# -gt 0 ]]; do case "$1" in --test) TEST_MODE=TRUE shift; ;; --endpoint) ENDPOINT_HOSTNAME=$2 shift 2 ;; --argaux) AUX=$2 shift 2 ;; esac done echo TEST_MODE=$TEST_MODE echo ENDPOINT_HOSTNAME=$ENDPOINT_HOSTNAME echo AUX=$AUX # getopt args END ``` **Note:** The `eval` in `eval set --$opts` is required as arguments returned by `getopt` are quoted. ## Example ```sh $ ./getopt.sh --test --endpoint "apple.com" --argaux "xyz" ``` ## Reference - http://www.bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt - https://dustymabe.com/2013/05/17/easy-getopt-for-a-bash-script/