Created
May 27, 2023 12:12
-
-
Save amalshaji/bd51ed19e58f4ff7f2030477a9203ef3 to your computer and use it in GitHub Desktop.
Replace template file environment placeholders with values from the environment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ $# -ne 2 ]; then | |
echo "Usage: $0 <input_file> <output_file>" | |
exit 1 | |
fi | |
input_file=$1 | |
output_file=$2 | |
template=$(cat "$input_file") | |
while IFS= read -r -d '' match; do | |
variable=${match:1} # Remove the leading '$' from the match | |
value=${!variable} # Get the value of the environment variable | |
template=${template//$match/$value} | |
done < <(echo "$template" | grep -o '\$[A-Za-z_][A-Za-z0-9_]*' | tr '\n' '\0') | |
echo "$template" > "$output_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Set the environment variables.
Create a new file,
input.txt
, with contents.Run the script,
This will produce output.txt,