Skip to content

Instantly share code, notes, and snippets.

@nyrahul
Created April 4, 2018 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyrahul/6e1917de8a255e92e47e2d129f57336b to your computer and use it in GitHub Desktop.
Save nyrahul/6e1917de8a255e92e47e2d129f57336b to your computer and use it in GitHub Desktop.
bash parse cmd args
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