initial commit
This commit is contained in:
47
replace_params.sh
Executable file
47
replace_params.sh
Executable file
@@ -0,0 +1,47 @@
|
||||
#!/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"
|
||||
|
||||
Reference in New Issue
Block a user