Last active
October 11, 2017 16:32
-
-
Save GiuseppeLota/9775b9508892ba15b55db6609bcc75e8 to your computer and use it in GitHub Desktop.
Bash getops template
This file contains 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 [-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