简体   繁体   中英

Replacing string via sed failing regardless of delimiter

I'm trying to inject a Jenkins param to a YAML file by substituting a dummy value with the database URL encoded as base64 but is failing by throwing: char 95: unterminated `s' command .

My variable looks like this:

env.DB_URL="jdbc:mariadb://mydb:3306/postservice?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&useSSL=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC"

Here's what I tried:

sed -e "s/dumyvalue/$(echo ${DB_URL} | base64)/" ./file.yaml

I tried to escape all characters including '/','&',';' but still no success. Also, I changed the delimiter to '#', same problem.

Expected results inside file.yaml:

database_url : dummyvalue should become database_url : db url base64 encoded

Works fine with strings without slashes.

I think the problem is with the spaces generated by the output of $(echo $DB_URL | base64) . Therefore, you could take a 2-step approach: First, sanitise the variable (by escaping the spaces for sed) before doing the substitution like so:

sanitised=$(echo $(echo $DB_URL | base64) | sed 's/ //g')

then 2) you can use it in the sed command: this should do the trick:

sed -e 's/dumyvalue/'"$sanitised"'/' ./file.yaml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM