Created
April 4, 2018 02:13
-
-
Save nyrahul/6e1917de8a255e92e47e2d129f57336b to your computer and use it in GitHub Desktop.
bash parse cmd args
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 characters
usage() | |
{ | |
echo "Usage: $0 <options>" | |
} | |
parse_cmdargs() | |
{ | |
# -s | --server is the short and long option to be parsed | |
# Remember to specify : in cases where argument is nessary both in short and long options | |
OPTS=`getopt -o s: --long server:xyz: -n 'parse-options' -- "$@"` | |
eval set -- "$OPTS" | |
while true; do | |
case "$1" in | |
-s | --server ) SRV_IP="$2"; shift 2;; | |
--xyz ) XYZ="$2"; shift 2;; | |
-- ) shift; break ;; | |
* ) break ;; | |
esac | |
done | |
[[ "$SRV_IP" == "" ]] && usage | |
echo "Server IP:$SRV_IP" | |
echo "XYZ:$XYZ" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment