|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Perl system $ variables
hi every1,
I need suggestion how interpolate variable($ip) to sysetm() or exec()? I tried different ways, but it doesn't add address. Code:
my $ip ='10.10.10.10' exec q( /usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr=$ip/g' /etc/pf.conf) ; system q(/usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr="$ip"/g' /etc/pf.conf); |
|
#2
|
||||
|
||||
|
use qq (double-quoted string) instead of q (single-quoted string)
|
|
#3
|
||||
|
||||
|
I tried this before, but received error
Substitution replacement not terminated at -e line 2 exec qq( /usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr=$ip/g' /etc/pf.conf) ; |
|
#4
|
|||
|
|||
|
You probably don't want that to interpolate...
Code:
exec qq( /usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr=$ip/g' /etc/pf.conf) ; (Incidentally, why start a new perl process instead of writing the loop in your existing one?)
__________________
sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh); |
|
#5
|
||||
|
||||
|
Quote:
Why? $ - just pointed on end of line and should works quite well. But if I set s/^external_addr=.*/...so on it still doesn't. Quote:
Because I'm too lazy so as to make a new loop =) I want do that like sed -i, because on different unix-system I've different sed version. Last edited by umbrella : July 5th, 2009 at 09:49 AM. |
|
#6
|
||||
|
||||
|
Quote:
Use sprintf to construct the string. Code:
my $ip ='10.10.10.10' exec sprintf q( /usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr=%s/g' /etc/pf.conf), $ip; system sprintf q(/usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr="%s"/g' /etc/pf.conf), $ip; |
|
#7
|
|||
|
|||
|
Quote:
Trying to be a bit more clear. The problem is when perl is parsing a quoted string, not when the system()'s perl parses the regex. Perl finds two variables in the string and interpolates the values of those values. Code:
qq( /usr/bin/perl -p -i -e 's/^external_addr.*$/external_addr=$ip/g' /etc/pf.conf) You want a literal $/ though, not the value of the record separator so you need to escape the $ and stop it from interpolating. |
|
#8
|
||||
|
||||
|
listen to the man ...
__________________
--Ax without exception, there is no rule ... Heavy Haulage Ireland Targeted Advertising Cookie Optout (TACO) extension for Firefox The great thing about Object Oriented code is that it can make small, simple problems look like large, complex ones ![]() 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski Detavil - the devil is in the detail, allegedly, and I use the term advisedly, allegedly ... oh, no, wait I did ... |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Perl system $ variables |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|