I would like to inject to in docker bash file 2nd line (after #!/bin/bash -x
) such a command: set -a && . .env && set +a
. I've tried this inside source.sh
:
TEXT_INSERT='set -a && . .env && set +a'
sed -i '1 $TEXT_INSERT' target.sh
but it does not work. How to do it?
I would like to inject to in docker bash file 2nd line (after #!/bin/bash -x
) such a command: set -a && . .env && set +a
. I've tried this inside source.sh
:
TEXT_INSERT='set -a && . .env && set +a'
sed -i '1 $TEXT_INSERT' target.sh
but it does not work. How to do it?
Share Improve this question edited Mar 31 at 20:33 Cyrus 89.2k15 gold badges108 silver badges166 bronze badges asked Mar 31 at 19:31 Ainz SamaAinz Sama 417 bronze badges 6 | Show 1 more comment2 Answers
Reset to default 1the `source.sh` file with content:
#!/bin/bash -x
TEXT_INN="set -a && . .env && set +a"
sed -i "1 a $TEXT_INN " target.sh
do the job
RUN $PGDATA/source.sh
with content
#!/bin/bash -x
TEXT_INN="set -a && . .env && set +a"
sed -i "2 a $TEXT_INN " /usr/local/bin/docker-entrypoint.sh
used in Dockerfile seems to override POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_DB, but I don't know where the ports set.
You could also use GNU awk
to insert the desired line after the line containing #!/bin/bash
:
$ awk -v text="${TEXT_INSERT}" '{if ($1 == "#!/bin/bash") {print $0 "\n" text} else {print}}' Dockerfile
sed
altogether here --cat <(printf '%s\n' 'set -a && . .env && set +a') target.sh >target.sh.new && mv target.sh{.new,}
does the job without requiring any nonstandard functionality (andsed -i
is not standardized). – Charles Duffy Commented Mar 31 at 19:38sed
a
command to append the line. – Barmar Commented Mar 31 at 19:44source.sh
an exact the same line insidetarget.sh
, but at the very beginning. – Ainz Sama Commented Mar 31 at 19:59sed -i "1 a $TEXT_INN " target.sh
insidesource.sh
works fine, after./source.sh
. double quotes do the thing :) – Ainz Sama Commented Mar 31 at 20:07cat
command I gave does the same thing, injecting a first-line value (and it does it the same waysed -i
does it, creating a whole new file and then renaming it over the target name). – Charles Duffy Commented Mar 31 at 21:01