My shell script looks like this:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1234 \
     -jar /xyz/abc-service-1.0-SNAPSHOT.jar \
     --server.port=1111 \
     --server.address= \
     --kafka.bootstrap.servers=,, \
     --topic.pushcrew.encryptionKey=abc-def-egh \
     --spring.application.name=name-sevice \
     --fb.verification.token=token \
     --management.trace.include.payload=true \
     --topic.pushcrew.hits=activity-logs \
     --max.block.ms=1000 \
     --pager.duty.api.key=apikey \

If I have to replace values of keys in the command, for ex: replacing value of key --server.address with the value to shell script through a variable, how would I do it?

I understand it would look like following if I had to replace value for key --server.address:

sed -i "s#--server.address=.* #$SERVER_ADDRESS#g;

But how do I maintain the space that has to be there after the replacement and how do I make sure that part int the key only after =(equals to) and before (space) gets replaced.


Negation to the rescue:

sed -i "s/server.address=[^\ ].*\ /server.address=${SERVER_ADDRESS} /g"

This is telling to search zero or more characters (.*) NOT matching whitespace ([^\ ]) up to the first whitespace.

A good pattern is not to use real values to be replaced ( but to use a mark like ###SERVER_ADDRESS###. If something goes wrong you can easily post-check with

grep "###" && exit -1

This can avoid continuing with dangerous values (mixing dev/pre/pro values)


You can do this using perl

For Example

perl -pi -e "s/server.address=localhost/server.address=${SERVER_ADDRESS}/g" file_path

This command change server address value localhost to vaariable value.

Hope this works

Not the answer you're looking for? Browse other questions tagged or ask your own question.