Last active
October 7, 2023 12:35
-
-
Save dejurin/5bdf60ab80f809dbd0ac09fe4d248b17 to your computer and use it in GitHub Desktop.
Usage: $0 SOURCE_FILE OUTPUT_FILE
This file contains 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 SOURCE_FILE OUTPUT_FILE" | |
exit 1 | |
fi | |
# Template file with placeholders | |
TEMPLATE_FILE="env.tmpl" | |
SOURCE_FILE="$1" | |
OUTPUT_FILE="$2" | |
# Check if the template file exists | |
if [ ! -f "$TEMPLATE_FILE" ]; then | |
echo "Template file $TEMPLATE_FILE not found." | |
exit 1 | |
fi | |
# Check if the source file exists | |
if [ ! -f "$SOURCE_FILE" ]; then | |
echo "Source file $SOURCE_FILE not found." | |
exit 1 | |
fi | |
# Read the template file into a variable | |
TEMPLATE_CONTENT=$(cat "$TEMPLATE_FILE") | |
# Read the template file into a variable | |
TEMPLATE_CONTENT2=$(cat "$SOURCE_FILE") | |
# Loop through each line in the source file | |
while IFS= read -r line; do | |
# Split the line into a key and value using '=' as the delimiter | |
key=$(echo "$line" | cut -d '=' -f 1) | |
value=$(echo "$line" | cut -d '=' -f 2-) | |
# Remove double quotes from the value if they exist | |
value=$(echo "$value" | sed 's/"//g') | |
# Use sed to replace the placeholders in the template with values | |
TEMPLATE_CONTENT=$(echo "$TEMPLATE_CONTENT" | sed "s|%$key%|$value|g") | |
done < "$SOURCE_FILE" | |
# Write the modified content to the output file | |
echo "$TEMPLATE_CONTENT" > "$OUTPUT_FILE" | |
echo "Created .env file." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment