- Published on
Compare Configuration Files
467 words3 min read
#!/bin/bash
EXAMPLE_FILE=local.env.example
LOCAL_FILE=local.env
if [[ ! -f "$LOCAL_FILE" ]]; then
echo "local environment file doesn't exist"
exit 0
fi
if [[ ! -f "$EXAMPLE_FILE" ]]; then
echo "example environment file doesn't exist"
exit 1
fi
LOCAL_CONTENTS=$(cat ./${LOCAL_FILE} | grep export | sed 's/^export //g' | awk -F'=' '{print $1}')
EXAMPLE_CONTENTS=$(cat ./${EXAMPLE_FILE} | grep export | sed 's/^export //g' | awk -F'=' '{print $1}')
ERRS=()
# Process contents of EXAMPLE_CONTENTS, line-by-line.
# IFS is empty as we don't want to separate the contents on each line
while IFS='' read -ra ADDR; do
for i in "${ADDR[@]}"; do
# If the env var is commented out, ignore it
if [ "${i:0:1}" == "#" ]; then
continue
fi
EXISTS_IN_EXAMPLE=$(echo "$EXAMPLE_CONTENTS" | grep "$i")
# If the string is null (empty)
if [ -z "$EXISTS_IN_EXAMPLE" ]; then
ERRS+=("$i")
fi
done
done <<< "$LOCAL_CONTENTS"
if [[ ! ${#ERRS[@]} -eq 0 ]]; then
echo "The following environment variables are defined in $LOCAL_FILE, but not in $EXAMPLE_FILE:"
echo "${ERRS[@]}"
exit 1
fi