Created
June 7, 2019 17:05
-
-
Save jvwong/5d317ce8e31f60951c8e68667b731d38 to your computer and use it in GitHub Desktop.
Bash script for parsing cmd line args
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
#!/bin/bash | |
dflag= | |
iflag= | |
oflag= | |
while getopts 'dio:' OPTION | |
do | |
case $OPTION in | |
d) dflag=1 | |
;; | |
i) iflag=1 | |
;; | |
o) oflag=1 | |
oval="$OPTARG" | |
if [ ! -d "$oval" ]; then | |
printf 'Option -o "%s" is not a directory\n' "$oval" | |
exit 2 | |
fi | |
;; | |
?) printf "Usage: %s [-d] [-i] [-o /path/to/output]\n" $0 >&2 | |
exit 2 | |
;; | |
esac | |
done | |
if [ "$dflag" ]; then | |
printf "Option -d specified\n" | |
fi | |
if [ "$iflag" ]; then | |
printf "Option -i specified\n" | |
fi | |
if [ "$oflag" ]; then | |
printf 'Option -o "%s" specified\n' "$oval" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment