Skip to content

Instantly share code, notes, and snippets.

@Wind010
Created July 28, 2023 01:12
Show Gist options
  • Save Wind010/8f0c45557422bff38fb11247e454c540 to your computer and use it in GitHub Desktop.
Save Wind010/8f0c45557422bff38fb11247e454c540 to your computer and use it in GitHub Desktop.
Bash script that parses a local.settings.json to export the properties as environment variables.
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <json_file>"
exit 1
fi
if [ ! -f "$1" ]; then
echo "Error: JSON file '$1' not found."
exit 1
fi
values_prop=$(jq -r '.Values' "$1")
declare -A kvp_arr
while IFS="|" read -r key value;
do
# Trim spaces to workaround.
key=$(echo "$key" | awk '{$1=$1};1')
# Check if the key contains a specific character (e.g., 'X')
if [[ "$key" == *"X"* ]]; then
continue
fi
# Check if the value is empty
if [ -z "$value" ]; then
continue
fi
# Remove the comma at the end of the value, if present
value=$(echo "$value" | sed 's/,$//')
#kvp_arr["$key"]="$value"
#export "$key"="$value"
echo "Exported: $key=$value"
done <<< "$(echo "$values_prop" | jq -r 'to_entries | map("\(.key)|\(.value|tostring)")')"
# This worked before we added the private key since it has new line characters it breaks but otherwise ok.
#declare -A kvp_arr
#while IFS="|" read -r key value;
#do
# kvp_arr["$key"]="$value"
# export "$key"="$value"
# echo "Exported: $key=$value"
#done <<< "$(echo "$values_prop" | jq -r 'to_entries | map("\(.key)|\(.value|tostring)") | .[]')"
# for key in "${!kvp_arr[@]}"
# do
# echo "$key = ${kvp_arr[$key]}"
# done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment