Created
November 27, 2020 14:21
-
-
Save xlbruce/23206547c92d808df27b180085773a42 to your computer and use it in GitHub Desktop.
Dummy script to parse arguments using `shift` bash built-in
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 | |
function usage { | |
cat <<EOF | |
Usage: $0 [-dv] <path> | |
EOF | |
exit 1 | |
} | |
verbose=0 | |
debug=0 | |
function parse_option { | |
local options="${1:1}" | |
while (( "${#options}" )); do | |
local option=${options:0:1} | |
case $option in | |
d) | |
echo "Debug mode activated" | |
debug=1;; | |
v) | |
let verbose="$verbose + 1" | |
echo "Verbose mode increased";; | |
esac | |
options=${options:1} | |
done | |
} | |
function parse_args { | |
while (( "$#" )); do | |
arg="$1" | |
shift | |
if [[ "${arg:0:1}" = "-" ]]; then | |
parse_option "$arg" | |
continue | |
fi | |
[ -e "$arg" -o -d "$arg" ] || usage | |
path="$arg" | |
echo "[DEBUG] 'path' is '$path'" | |
done | |
[ -z "$path" ] && usage | |
} | |
parse_args $@ | |
echo verbose is $verbose |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment