Created
November 25, 2018 09:33
-
-
Save jbardon/1a3aa5fa857e7cb7e04e5b0bc42b6dbb to your computer and use it in GitHub Desktop.
Traitement automatique des options bash
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
myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile | |
myscript -v -f -d -o/fizz/someOtherFile -- ./foo/bar/someFile | |
myscript --verbose --force --debug ./foo/bar/someFile -o/fizz/someOtherFile | |
myscript --output=/fizz/someOtherFile ./foo/bar/someFile -vfd | |
myscript ./foo/bar/someFile -df -v --output /fizz/someOtherFile | |
verbose: y, force: y, debug: y, in: ./foo/bar/someFile, out: /fizz/someOtherFile | |
#!/bin/bash | |
# saner programming env: these switches turn some bugs into errors | |
set -o errexit -o pipefail -o noclobber -o nounset | |
! getopt --test > /dev/null | |
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then | |
echo "I’m sorry, `getopt --test` failed in this environment." | |
exit 1 | |
fi | |
OPTIONS=dfo:v | |
LONGOPTS=debug,force,output:,verbose | |
# -use ! and PIPESTATUS to get exit code with errexit set | |
# -temporarily store output to be able to check for errors | |
# -activate quoting/enhanced mode (e.g. by writing out “--options”) | |
# -pass arguments only via -- "$@" to separate them correctly | |
! PARSED=$(getopt --options=$OPTIONS --longoptions=$LONGOPTS --name "$0" -- "$@") | |
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then | |
# e.g. return value is 1 | |
# then getopt has complained about wrong arguments to stdout | |
exit 2 | |
fi | |
# read getopt’s output this way to handle the quoting right: | |
eval set -- "$PARSED" | |
d=n f=n v=n outFile=- | |
# now enjoy the options in order and nicely split until we see -- | |
while true; do | |
case "$1" in | |
-d|--debug) | |
d=y | |
shift | |
;; | |
-f|--force) | |
f=y | |
shift | |
;; | |
-v|--verbose) | |
v=y | |
shift | |
;; | |
-o|--output) | |
outFile="$2" | |
shift 2 | |
;; | |
--) | |
shift | |
break | |
;; | |
*) | |
echo "Programming error" | |
exit 3 | |
;; | |
esac | |
done | |
# handle non-option arguments | |
if [[ $# -ne 1 ]]; then | |
echo "$0: A single input file is required." | |
exit 4 | |
fi | |
echo "verbose: $v, force: $f, debug: $d, in: $1, out: $outFile" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment