Justin808
Justin808

Reputation: 21512

String replacement with Perl from inside a Makefile

I'm trying to replace a string, inside of a file, with perl from inside a Makefile.

InstallTo = $(PWD)/WebTest

BuildApache:
    mkdir -p WebTest
    cd Source/httpd; ./configure --prefix=$(InstallTo) --exec-prefix=$(InstallTo)
    cd Source/httpd; make; make install
    cd $(InstallTo)/conf; perl -pi -e 's/ServerRoot \"$(InstallTo)\"/ServerRoot/g' httpd.conf
    cd $(InstallTo)/conf; cp -f httpd.conf httpd.conf.orig

I'm not sure exactly what I'm doing though, I've just tried to modify the perl line from something I found on the net. I think its the \" thats messing things up but I don't know enough about Perl to fix it.

Upvotes: 0

Views: 491

Answers (2)

SAN
SAN

Reputation: 2247

Since your variable contains '/' you need to use a different character for regular expressions, also you may want to use quotemeta or \Q..\E in regular expressions having variables which can contain special characters

s#\QServerRoot "$(InstallTo)"\E#ServerRoot#g

Refer to this post for more details how-do-i-handle-special-characters-in-a-perl-regex

Upvotes: 0

Axeman
Axeman

Reputation: 29844

You might want to try:

s|ServerRoot "$(InstallTo)"|ServerRoot|g

You're pasting a value with a slash in it as part of the search expression. It ends up as:

s/ServerRoot \"PWD/WebTest\"/ServerRoot/g

(Where PWD stands for any literal directory spec.) Since you can't escape the slash, that's always going to be a problem unless you use an alternative delimiter.

Upvotes: 2

Related Questions