MJB
MJB

Reputation: 9399

Sed expansion fails

During a setup program, a configuration file (log4j.properties) needs to be modified.

One line of log4j.properties contains

log4j.appender.R.File=${catalina.base}/logs/OurProductWorker.log

We want it to be changedto

log4j.appender.R.File=${catalina.base}/logs/OurProductWorker5.log

(or some similar number)

This works:

sed -i 's/OurProductWorker.log/'"$1.log"'/g' "log4j.properties"

($1 = a number from 1 to 9 in the function called)

This fails:

B_PRODUCT=OurProduct 
sed -i 's/"$B_PRODUCT"Worker.log/'"$1.log"'/g' "log4j.properties"

I need to use a variable because we need to be to rebrand this for OEMs, and it's obviously easier to just change a bunch of variables at the beginning of the script, rather than individual functions.

Upvotes: 1

Views: 62

Answers (1)

shellter
shellter

Reputation: 37288

Try this

sed -i 's/'"$B_PRODUCT"'Worker.log/'"$1.log"'/g' "log4j.properties"

You always have to 'turn off' the single-quotes to have variable expand (as you have done with $1).

I hope this helps.

Upvotes: 2

Related Questions