In my dockerized container I am unable to replace a variable using ‘sed’.
These are my files:
myfile.sql
CREATE TABLE IF NOT EXISTS ${PG_TABLE}(...)
myscript.sh
#!/bin/sh
echo "variable1=${1}"
echo "variable2=${2}"
sed -i "s/${$1}/$2/g" myfile.sql
run command
myscript.sh PG_TABLE "mytablename"
Actual:
- echo variable1=PG_TABLE
- echo variable2=mytablename
- REPLACEMENT:
CREATE TABLE IF NOT EXISTS (…)
Expected:
- echo variable1=PG_TABLE
- echo variable2=mytablename
- REPLACEMENT
CREATE TABLE IF NOT EXISTS mytablename(…)
This is supposed to replace the placeholder with my variable ‘mytablename’, but it just replaces it with empty string.
Maybe its because of my container alpine version.
This is my os.
my docker build operating system
cat /etc/os-release
- PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
- NAME="Debian GNU/Linux"
- VERSION_ID="12"
- VERSION="12 (bookworm)"
- VERSION_CODENAME=bookworm
- ID=debian
2
Answers
ok I made a mistake. The reason it didn't work is because I was expecting the arg to exist in my docker-compose file and it wasn't. Therefore the replacement was empty "". Adding in that arg
docker-compose.yml
Dockerfile
Issue is with
sed
placeholder.Use ${${1}} to match the placeholder in the myfile.sql
Here’s the working solution, where running the container shows correct replacement.
myscript.sh
myfile.sql
Dockerfile
OUTPUT