Skip to content

Instantly share code, notes, and snippets.

@GiuseppeLota
Last active October 11, 2017 16:32
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 GiuseppeLota/9775b9508892ba15b55db6609bcc75e8 to your computer and use it in GitHub Desktop.
Save GiuseppeLota/9775b9508892ba15b55db6609bcc75e8 to your computer and use it in GitHub Desktop.
Bash getops template
usage() { echo "Usage: $0 [-s <45|90>] [-p <string>]" 1>&2; exit 1; }
while getopts ":s:p:" o "$@";
do
case $o in
s) s=${OPTARG}
((s == 45 || s == 90)) || usage;;
p)p=${OPTARG};;
*) usage ;;
esac
done
shift $((OPTIND-1))
if [ -z "${s}" ] || [ -z "${p}" ]; then
usage
fi
echo "s = ${s}"
echo "p = ${p}"
# test command: -s: check if string is empty
# -f: check if file exist
# -s: check not empty
# -ne: not equal (ex: INTEGER1 -ne INTEGER2)
# -z: the length of STRING is zero
# 1>&2
# 1 = standard output
# 2 = standard error
# "$@" - optional positional parameters except $0
# "$#" - the number of arguments, not counting $0
# "$2" - second positional parameter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment