Jonas Stock
Jonas Stock

Reputation: 1

Replace string with $ from makefile with sed

I am trying to replace ${dbPassword} in a property file with a password including $ signs.

My command is the following, but I have no idea how to replace this properly.

run: @cat $(FILE_PATH) | sed -i .bak 's/$${dbPassword}/$(VARIABLE_WITH_PWD)/g' $(FILE_PATH)

Let's say my dbPassword is: 123$456$789

With this, I am getting the result: 123${dbPassword}456${dbPassword}789

Upvotes: 0

Views: 299

Answers (1)

Jens
Jens

Reputation: 72657

Three problems:

  1. Target and recipe on the same line. The recipe must be on the next line, indented with a hard tab.
  2. You pass $(FILE_PATH) on stdin, and as a file argument to sed. Remove the cat pipe.
  3. Shell variables (as opposed to make variables) are not substituted inside single quotes.

So you might want to try this instead:

.PHONY: run
run:
        sed -i .bak "s/$${dbPassword}/$(VARIABLE_WITH_PWD)/g" $(FILE_PATH)

This assumes you have an environment variable, dbPassword and exported it prior to running make. If that is not the case, please provide your complete makefile.

I also have removed the @ so you actually see what command make is executing. There's no point in wearing a blindfold while debugging your makefile.

Upvotes: 1

Related Questions