Created
August 31, 2018 00:01
-
-
Save jay74jung/fc8881daede6b70b740e8eec77f89fb2 to your computer and use it in GitHub Desktop.
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/sh | |
# POSIX | |
# getopts | |
# Usage info | |
show_help() { | |
cat << EOF | |
Usage: ${0##*/} [-hv] [-f OUTFILE] [FILE]... | |
Do stuff with FILE and write the result to standard output. With no FILE | |
or when FILE is -, read standard input. | |
-h display this help and exit | |
-f OUTFILE write the result to OUTFILE instead of standard output. | |
-v verbose mode. Can be used multiple times for increased | |
verbosity. | |
EOF | |
} | |
# Initialize our own variables: | |
output_file="" | |
verbose=0 | |
OPTIND=1 | |
# Resetting OPTIND is necessary if getopts was used previously in the script. | |
# It is a good idea to make OPTIND local if you process options in a function. | |
while getopts hvf: opt; do | |
case $opt in | |
h|\?|--help) | |
show_help | |
exit 0 | |
;; | |
v) verbose=$((verbose+1)) | |
;; | |
f) output_file=$OPTARG | |
;; | |
*) | |
show_help >&2 | |
exit 1 | |
;; | |
esac | |
done | |
shift "$((OPTIND-1))" # Discard the options and sentinel -- | |
# Everything that's left in "$@" is a non-option. In our case, a FILE to process. | |
printf 'verbose=<%d>\noutput_file=<%s>\nLeftovers:\n' "$verbose" "$output_file" | |
printf '<%s>\n' "$@" | |
# End of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment