Operating System - Linux
1844878 Members
1514 Online
110233 Solutions
New Discussion

programming and scripting variable substitution question

 
SOLVED
Go to solution
rmueller58
Valued Contributor

programming and scripting variable substitution question

All,

Attempting to setup a installation/configuration script to quicking blow down a complex setup of applications on RH9.

I have the "software download and installation" complete however, I still have several files that require editing, I would prefer to add these edits to my script using inline editing with perl or sed..

for example:

I have a "/etc/foo/foo.conf"

with in the foo.conf

I have several variables that are set usually manually with vi or emacs,
For example I have a variable with a path attached..
var FOOCONF_PATH ../fooconf

I want to change the FOO_PATH in the foo.conf to a full path

var FOOCONF_PATH /etc/foo/fooconf

I've tried the following:

export FOOPATHOLD "var FOOCONF_PATH ../fooconf"
echo "Enter the new path for FOO Configs"
read $FOOPATHNEW
export FOOPATHNEW
///then I attempt to inline edit using variable substitutions.

I am doing as follows:

cp -p foo.conf foo.conf.orig

perl -p -i -e s/$FOOPATHOLD/$FOOPATHNEW/g foo.conf

the substitution flakes out on me..
I've tried:
perl -p -i -e s/"$FOOPATHOLD"/"$FOOPATHNEW"/g foo.conf

Any thoughts appreciated..


Rex M - Omaha NE



6 REPLIES 6
delahaye_1
New Member

Re: programming and scripting variable substitution question

s/FOOPATHOLD/$FOOPATHNEW/g
Alexander Chuzhoy
Honored Contributor

Re: programming and scripting variable substitution question

How about using File::Basename module?
dirname and basename will be usefull here...
rmueller58
Valued Contributor

Re: programming and scripting variable substitution question

Alex not familiar with the module, I will look into it

An down and dirty examples you can throw at me?
Stuart Browne
Honored Contributor
Solution

Re: programming and scripting variable substitution question

Simply put, your issue is with slashes.

Here's an example:

FOOPATH="var FOOCONF_PATH ../fooconf"
FOOPATHNEW="var FOOCONF_PATH /etc/foo/fooconf"

perl -pi -e "s/$FOOPATH/$FOOPATHNEW/g" foo.conf

Expand the -e, and you get:

"s/var FOOCONF_PATH ../fooconf/var FOOCONF_PATH /etc/foo/fooconf/g"

Way broken.

Using something like:

perl -pi -e "s#$FOOPATH#$FOOPATHNEW#g" foo.conf

works just fine however.
One long-haired git at your service...
rmueller58
Valued Contributor

Re: programming and scripting variable substitution question

Stu,
using the "#" is new for me..

Need to get my regexp book out, I tried "!" yesterday, I think I got it now.. Thanks!!!

s#$FOOPATHOLD#$FOONEWPATH# worked.

Rex
Stuart Browne
Honored Contributor

Re: programming and scripting variable substitution question

You can actually use *any* character. Go Regular Expressions. It's in the documentation somewhere (I read it once upon a time), but can't find it at the moment.

It's just usual to use /'s as they are the default.
One long-haired git at your service...