admin管理员组文章数量:1384014
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
本文标签: insert line with command from one bash file to anotherStack Overflow
版权声明:本文标题:insert line with command from one bash file to another - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743925058a2562806.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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