Skip to content

Instantly share code, notes, and snippets.

@jvwong
Created June 7, 2019 17:05
Show Gist options
  • Save jvwong/5d317ce8e31f60951c8e68667b731d38 to your computer and use it in GitHub Desktop.
Save jvwong/5d317ce8e31f60951c8e68667b731d38 to your computer and use it in GitHub Desktop.
Bash script for parsing cmd line args
#!/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