Makefile tutorial replace strings in shell script from environment file
This refinement specifies the context of using a Makefile and emphasizes the action of replacing strings in a shell script, which will yield more focused and relevant results on tutorials and examples.
To create a Makefile that replaces strings inside a .sh file using values from an .env file, we will leverage shell utilities like sed for substitution, while Makefile rules will help manage this process efficiently. Here’s a step-by-step guide to achieving this.
.env FileTypically, an .env file is formatted with key-value pairs that represent environment variables. For instance:
# .env file
DATABASE_URL=mysql://user:password@localhost/dbname
API_KEY=secret_api_key
.sh ScriptLet’s assume you have a shell script file named script.sh that contains placeholder values for these variables:
#!/bin/bash
echo "Connecting to database at ${DATABASE_URL}"
echo "Using API Key: ${API_KEY}"
The Makefile will read the .env file, export those variables, and use them to replace placeholders in script.sh.
Below is an example of how to set up your Makefile to replace strings in the .sh file based on the values from the .env file:
# Makefile
# Define the target shell script and the env file
SCRIPT = script.sh
ENV_FILE = .env
# Default target
all: replace
# Target to replace strings in the shell script
replace: $(ENV_FILE)
@echo "Replacing placeholders in $(SCRIPT) using $(ENV_FILE)..."
@set -a; . $(ENV_FILE); set +a; \
sed -e "s|\${DATABASE_URL}|$$DATABASE_URL|g" \
-e "s|\${API_KEY}|$$API_KEY|g" \
$(SCRIPT) > tmp_script.sh && \
mv tmp_script.sh $(SCRIPT) && \
echo "Replacements done."
# Clean target
clean:
rm -f $(SCRIPT)
.PHONY: all replace clean
Variables: SCRIPT and ENV_FILE are defined as the names of your shell script and environment file, respectively.
Default Target: The all target, which is invoked when you run make, depends on the replace target.
Replace Target: This contains the commands that will:
.env using the set -a and set +a commands, enabling the export of variables for use within the same shell.sed with the -e flag to perform regex-based replacements in script.sh. The g at the end of each sed command option indicates a global replacement of the placeholders.tmp_script.sh), which is then moved back to the original script.sh.Clear Target: This optional clean target can be used to remove the original .sh file, in case you need to reset your script before running replacements again.
To execute the replacement process:
.env file and script.sh in the same directory as your Makefile.make
After running this command, your script.sh should now have the actual values from the .env file embedded in it.
This approach allows you to dynamically replace placeholders in your shell scripts with actual values defined in an environmental file. By utilizing Makefiles, you can streamline your build and configuration processes effectively, making your deployments more consistent and manageable.
Feel free to modify the variables and commands in the Makefile according to your specific requirements to enhance its functionality, such as handling additional environment variables or scripts.