Skip to content

Instantly share code, notes, and snippets.

@goddoe
Created June 13, 2022 12:35
Show Gist options
  • Select an option

  • Save goddoe/c696435a51622800e75379facb6ff9c9 to your computer and use it in GitHub Desktop.

Select an option

Save goddoe/c696435a51622800e75379facb6ff9c9 to your computer and use it in GitHub Desktop.
bash option
#!/bin/bash
# Reference: https://www.baeldung.com/linux/bash-parse-command-line-arguments
VALID_ARGS=$(getopt -o abg:d: --long alpha,beta,gamma:,delta: -- "$@")
if [[ $? -ne 0 ]]; then
exit 1;
fi
eval set -- "$VALID_ARGS"
while [ : ]; do
case "$1" in
-a | --alpha)
echo "Processing 'alpha' option"
shift
;;
-b | --beta)
echo "Processing 'beta' option"
shift
;;
-g | --gamma)
echo "Processing 'gamma' option. Input argument is '$2'"
shift 2
;;
-d | --delta)
echo "Processing 'delta' option. Input argument is '$2'"
shift 2
;;
--) shift;
break
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment