#!/bin/bash # Parse command line arguments while [[ $# -gt 0 ]]; do key="$1" case $key in -t|--template-dir) TEMPLATE_DIR="$2" shift shift ;; -c|--config-file) CONFIG_FILE="$2" shift shift ;; *) echo "Unknown option: $key" exit 1 ;; esac done # If the TEMPLATE_DIR or CONFIG_FILE variables were not set from the command line, # read them from a configuration file if [[ -z $TEMPLATE_DIR ]]; then source "/path/to/config" fi if [[ -z $CONFIG_FILE ]]; then source "/path/to/config" fi # Loop over all files in the template directory while IFS= read -r -d '' file; do # Use sed to replace all placeholders with values from the config file while IFS='=' read -r key value; do key_escaped=$(printf '%s\n' "$key" | sed -e 's/[]\/$*.^|[]/\\&/g') value_escaped=$(printf '%s\n' "$value" | sed -e 's/[\/&]/\\&/g') sed -i "s/{{ $key_escaped }}/$value_escaped/g" "$file" done < "$CONFIG_FILE" done < <(find "$TEMPLATE_DIR" -type f -print0)