我正在使用shell命令执行shell命令,以查找和替换配置文件中的内容。我的配置是像下面这样的文件内容,我想通过我的诊所的名称找到所有的字符串"“(这是我从客户端传递的一个参数,如: var clinicName = req.body.clinicName;)
version: '3.2'
services:
# core backend service
core-backend:
image: "vany/multi-tenant-core:latest"
deploy:
replicas: 1
placement:
constraints: [node.labels.tenant==multi-tenant]
restart_policy:
condition: any
labels:
collectord.io/logs-eventpattern: '^\['
environment:
PORT: 5000
NODE_ENV: 'production'
DATABASE: 'mongodb://<tclinic-name>:yeuemdailau@10.117.134.9:27017/<tclinic-name>?authSource=<tclinic-name>'
volumes:
- /etc/localtime:/etc/localtime:ro
#volumes:
# media-upload:
# driver: lizardfs如果我使用这样的手动命令,结果是正确的。
sed -i "s/<tclinic-name>/hanoitclinic/g" clinic-api-core.yml这样的结果是正确的:
version: '3.2'
services:
# core backend service
core-backend:
image: "vany/multi-tenant-core:latest"
deploy:
replicas: 1
placement:
constraints: [node.labels.tenant==multi-tenant]
restart_policy:
condition: any
labels:
collectord.io/logs-eventpattern: '^\['
environment:
PORT: 5000
NODE_ENV: 'production'
DATABASE: **'mongodb://hanoitclinic:yeuemdailau@10.117.134.9:27017//hanoitclinic?authSource=hanoitclinic'**
volumes:
- /etc/localtime:/etc/localtime:ro
#volumes:
# media-upload:
# driver: lizardfs但是,如果我使用的是shelljs,那么这样的代码(使用从客户端传递的clinicName = "hanoitclinic“)
var shell = require('shelljs');
shell.sed('-i', '<tclinic-name>', clinicName, "clinic-api-core.yml");结果不正确,只替换了一次:
DATABASE: 'mongodb://hanoitclinic:yeuemdailau@10.117.134.9:27017/<tclinic-name>?authSource=<tclinic-name>'请看一下。谢谢
发布于 2021-03-11 16:29:02
根据ShellJS 文档和这里中的例子
sed([options,] search_regex, replacement, file [, file ...])search_regex可以是一个正常的replace() regexp,所以我建议使用global标志:
shell.sed('-i', /<tclinic-name>/g, clinicName, "aibolit-api-core.yml");https://stackoverflow.com/questions/66576391
复制相似问题