schon/scripts/Unix/export-environment-file.sh
Egor fureunoir Gorbunov 0dc3daaa4f Features: 1) Add fallback for missing jq in image verification; 2) Enhance verbosity control for management commands in run.sh; 3) Update generate-environment-file.sh to use consistent variable names and handle CORS origins dynamically.
Fixes: 1) Correct `.env` export script to handle sorted variables without associative arrays; 2) Fix incomplete `.env` output in `generate-environment-file.sh`.

Extra: 1) Improve code readability and maintainability across shell scripts; 2) Standardize echo statements and reduce excessive formatting.
2025-10-28 15:31:26 +03:00

34 lines
1 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
source ./scripts/Unix/starter.sh
env_file=".env"
if [ ! -f "$env_file" ]; then
echo ".env file not found in the current directory." >&2
exit 1
fi
# Read .env and export variables; collect for sorted output (portable: no associative arrays)
mapfile -t lines < <(sed -e 's/^\s*//;s/\s*$//' "$env_file" | awk 'NF && $0 !~ /^#/ && $0 ~ /=/')
kv_list=()
for raw in "${lines[@]}"; do
key=${raw%%=*}
value=${raw#*=}
# Trim whitespace around key and value
key=$(echo "$key" | sed -e 's/^\s*//' -e 's/\s*$//')
value=$(echo "$value" | sed -e 's/^\s*//' -e 's/\s*$//')
# Strip matching quotes
if [ -n "$value" ] && [ "${value:0:1}" = '"' ] && [ "${value: -1}" = '"' ]; then
value=${value:1:${#value}-2}
elif [ -n "$value" ] && [ "${value:0:1}" = "'" ] && [ "${value: -1}" = "'" ]; then
value=${value:1:${#value}-2}
fi
export "$key=$value"
kv_list+=("$key=$value")
done
if [ ${#kv_list[@]} -gt 0 ]; then
printf "%s\n" "${kv_list[@]}" | sort | tr "\n" ";" | sed 's/;$/\n/'
fi