Last active
September 16, 2018 01:04
-
-
Save mattolenik/5a51ab685bded0cf542b7524a690252d to your computer and use it in GitHub Desktop.
Looks for "named arguments" in an array, which take the form key=value
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
# Searches for a named argument in the form of "key=value". | |
# Returns the value associated with the key. | |
# | |
get_named_arg() { | |
local key="$1" | |
local default="${2:-}" | |
shift 2 | |
for pair in "$@"; do | |
if [[ $pair =~ ^([^=]+)=([^=]*)$ ]] && [[ ${BASH_REMATCH[1]} == "$key" ]]; then | |
printf %s "${BASH_REMATCH[2]}" | |
fi | |
done | |
printf %s "$default" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment