Skip to content

Instantly share code, notes, and snippets.

@chrisguest75
Created May 25, 2021 13:27
Show Gist options
  • Select an option

  • Save chrisguest75/b6bf4770237e1307b3fef4ffa3d4a187 to your computer and use it in GitHub Desktop.

Select an option

Save chrisguest75/b6bf4770237e1307b3fef4ffa3d4a187 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -ef -o pipefail
readonly SCRIPT_NAME=$(basename "$0")
readonly SCRIPT_PATH=${0}
# shellcheck disable=SC2034
readonly SCRIPT_DIR=$(dirname "$SCRIPT_PATH")
(return 0 2>/dev/null) && SOURCED=1 || SOURCED=0
if [[ $SOURCED == 1 ]]; then
#echo "Script is being sourced"
return 1
#else
#echo "Script is a subshell"
fi
STRING_TO_FIND=""
# Using ${1-} means if we don't set $1 it doesn't fail with unbound variable
if [[ -n ${1-} ]]; then
STRING_TO_FIND=$1
else
echo "No account provided. '$SCRIPT_NAME <account> <optional mapfile>'"
exit 1
fi
#MAP_FILE="map.conf"
if [[ -n ${2-} ]]; then
MAP_FILE=$2
fi
if [[ -z ${MAP_FILE-} ]]; then
echo "No MAP_FILE envvar provided or '$SCRIPT_NAME <account> <optional mapfile>'"
exit 1
fi
function trim() {
: ${1?"${FUNCNAME[0]}(string) - missing string argument"}
if [[ -z ${1} ]]; then
echo ""
return
fi
# remove an
local trimmed=${1##*( )}
echo ${trimmed%%*( )}
}
EXITCODE=1
while IFS=, read -r name account role
do
name=$(trim "$name")
role=$(trim "$role")
account=$(trim "$account")
if [[ "$name" == "$STRING_TO_FIND" ]]; then
echo "$role"
EXITCODE=0
fi
done < ${MAP_FILE}
exit ${EXITCODE}
{
"name": "@chrisguest75/get_mapped_value.sh",
"version": "0.0.1",
"description": "",
"main": "get_mapped_value.sh",
"bin": "get_mapped_value.sh",
"scripts": {
"start": "./get_mapped_value.sh",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment