1752478 Members
5623 Online
108788 Solutions
New Discussion

Variable probleme

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

Variable probleme

Hi

Here my call

find /su01/opus/html/*.html -type f -exec perl -i -pe 's|\Q’$ENVSOURCE’\E|’$ENVDEST’|g' {} \;


The problem is that $ENVSOURCE and $ENVDEST are never evaluated. Why ?

Bests Regards
Den
7 REPLIES 7
Leo The Cat
Regular Advisor

Re: Variable probleme

I have tried ${ENVSOURCE} without success
I have tried $($ENVSOURCE) without success
Leo The Cat
Regular Advisor

Re: Variable probleme

Important note


find /su01/opus/html/*.html -type f -exec perl -i -pe 's|\Qâ $ENVSOURCEâ \E|â $ENVDESTâ |g' {} \;

Works perfectly in a command line mode.

The problem is when I have trued to put this command in a ksh script !


Bests regards
Den

Steven Schweda
Honored Contributor

Re: Variable probleme

Some of those characters don't copy+paste on
my system, so I can't tell what they really
are, but inside apostrophes, variables are
not evaluated. For example:

alp$ E='a b c'
alp$ echo ${E}
a b c
alp$ echo "${E}"
a b c
alp$ echo '${E}'
${E}



"man sh"? Buy a book?
Leo The Cat
Regular Advisor

Re: Variable probleme

Sorry but I have no time to become a caid on sh ! It's because I come here !
Ivan Ferreira
Honored Contributor

Re: Variable probleme

As methioned above, replace single quotes by double quotes if you can. Single quotes prevents the variables from being subtituted.

Probably, you should write a perl script and call the script from the find command. I don't know much about perl.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
James R. Ferguson
Acclaimed Contributor
Solution

Re: Variable probleme

Hi Den:

The $ENVSOURCE and $ENVDEST are Perl's variables, not the shell's. All you need do is this:

# export ENVSOURCE=oldthing
# export ENVDEST=newthing

# perl -i.old -pe 'BEGIN{$ENVSOURCE=$ENV{ENVSOURCE};$ENVDEST=$ENV{ENVDEST};};s|\Q.$ENVSOURCE.\E|.$ENVDEST.|g' file

Thus we export to the environment in which Perl will run and then tell Perl to use the exported environmental variables.

Obviously you can couple this with your shell find() as you wrote. You could also use Perl's 'File::Find' module directory.

Regards!

...JRF...
Leo The Cat
Regular Advisor

Re: Variable probleme

James and Ivan give me the good way.
Thanks Guys