我正在尝试从docker compos文件执行sed命令。该命令希望匹配文件中的某些文本,然后将下一行文本替换为文本我正在执行的命令是:
command: bash -c "sed ''/Http Basic Authentication Filter/!b;n;c<filter>'' webapps/engine-rest/WEB-INF/web.xml > webapps/engine-rest/WEB-INF/noauth.xml"
我可以直接在 bash 中运行该命令,它按预期工作,但是运行 docker-up 我得到:
sed: unmatched '/'
bash: n: command not found
bash: filter: No such file or directory
所以我假设这是一个 YAML 字符转义问题,但我似乎无法让它工作。
我还尝试了以下方法以避免转义字符:
command: >
bash -c sed '/Http Basic Authentication Filter/!b;n;c<filter>' webapps/engine-rest/WEB-INF/web.xml > webapps/engine-rest/WEB-INF/noauth.xml
您不需要转义双引号内的单引号。 双单引号 (sic( 逐字传递,产生/Http
,如sed
看到的第一个字符串,显然缺少右斜杠。
如果字符串括在单引号中,则 YAML 引用将需要双单引号;但是您使用了双引号,因此值中的单引号不需要转义(并且在 shell 删除相邻的空引号对后,尝试将有效地导致没有引号(。