最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

insert line with command from one bash file to another - Stack Overflow

programmeradmin1浏览0评论

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
  • 2 Expansions only happen inside double quotes, not single quotes. That said, I'd probably avoid 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 (and sed -i is not standardized). – Charles Duffy Commented Mar 31 at 19:38
  • 2 You're missing the sed a command to append the line. – Barmar Commented Mar 31 at 19:44
  • @CharlesDuffy it works very good injecting from source.sh an exact the same line inside target.sh , but at the very beginning. – Ainz Sama Commented Mar 31 at 19:59
  • @Barmar - thx, the line content sed -i "1 a $TEXT_INN " target.sh inside source.sh works fine, after ./source.sh. double quotes do the thing :) – Ainz Sama Commented Mar 31 at 20:07
  • @AinzSama, yes, that cat command I gave does the same thing, injecting a first-line value (and it does it the same way sed -i does it, creating a whole new file and then renaming it over the target name). – Charles Duffy Commented Mar 31 at 21:01
 |  Show 1 more comment

2 Answers 2

Reset to default 1

the `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
发布评论

评论列表(0)

  1. 暂无评论