Files
polyamorphism_optimization/replace_params.sh
2025-08-09 13:43:03 +02:00

48 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
if [[ $# -lt 2 ]]; then
echo "Usage: $0 <params.ini> <template> [--output <output_file>]"
exit 1
fi
PARAMS_FILE="$1"
TEMPLATE_FILE="$2"
OUTPUT_FILE="${TEMPLATE_FILE}.out"
# Optional --output <filename>
if [[ "$3" == "--output" && -n "$4" ]]; then
OUTPUT_FILE="$4"
fi
declare -A PARAMS
# Load params.ini (ignore [section] headers)
while IFS='=' read -r key value; do
# Trim whitespace and ignore comments/sections
key="${key%%\#*}" # remove inline comments
value="${value%%\#*}"
key="$(echo "$key" | xargs)" # trim leading/trailing whitespace
value="$(echo "$value" | xargs)"
[[ -z "$key" || "$key" =~ ^\[.*\]$ ]] && continue
key_upper=$(echo "$key" | tr '[:lower:]' '[:upper:]')
PARAMS["$key_upper"]="$value"
done < "$PARAMS_FILE"
# Read template into a variable
template=$(<"$TEMPLATE_FILE")
# Replace placeholders (only when surrounded by spaces)
for key in "${!PARAMS[@]}"; do
val="${PARAMS[$key]}"
# Use sed to do space-surrounded substitution
template=$(echo "$template" | sed -E "s/([[:space:]])$key([[:space:]])/$val/g")
done
# Save to output file
echo "$template" > "$OUTPUT_FILE"
echo "Wrote output to $OUTPUT_FILE"