Skip to content

Instantly share code, notes, and snippets.

@binodluitel
Created January 2, 2018 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binodluitel/efd9d9821307b2b39ce63eb9bb029757 to your computer and use it in GitHub Desktop.
Save binodluitel/efd9d9821307b2b39ce63eb9bb029757 to your computer and use it in GitHub Desktop.
create/use map in shell script
#!/usr/bin/env bash
# put a key/value in map
# usage: map_put map_name key value
map_put() {
[ "$#" != 3 ] && exit 1
alias "${1}$2"="$3"
}
# get a value from map
# usage: map_get map_name key
map_get() {
[ "$#" != 2 ] && exit 1
if map_has_key $1 $2; then
alias "${1}$2" | awk -F"'" '{ print $2; }'
fi
}
# get all keys from map
# returns array of keys
# usage: map_keys map_name
map_keys() {
[ "$#" != 1 ] && exit 1
alias -p | grep $1 | cut -d'=' -f1 | awk -F"$1" '{print $2; }'
}
# check if map has a key
# usage: map_has_key map_name key
map_has_key() {
[ "$#" != 2 ] && exit 1
if alias ${1}$2 2>/dev/null>/dev/null; then
return 0
fi
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment