s_gaurav1091 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I am trying to substitute a value in a file through perl. my code is
$sdpType = "sdp2"; $version = "1.0.0.0"; system("rsh $sdpType perl -pi -e 's\/^(command\.package)\=(\\S+)\/\"\ +$1\=$version\"\/gei' $ENV{JBOSS_HOME}/install/version.properties");
But I am getting follwing error
ksh: 0403-057 Syntax error at line 1 : `(' is not expected.
The same thing works without the rsh, but when I am giving rsh it's throwing an error. Can anyone plz help as to what I should do.Thanx in advance

Replies are listed 'Best First'.
Re: substituting a regular expression in a file
by chargrill (Parson) on Sep 06, 2006 at 13:05 UTC

    Does your command line work without being wrapped in Perl?

    rsh sdp2 perl -pi -e 's\/^(command\.package)\=(\\S+)\/\"\$1\=1.0.0.0\" +\/gei' $ENV{JBOSS_HOME}/install/version.properties

    I'd bet you have a quoting/interpolation issue, and getting it to work without being wrapped in a Perl system call might get you on your way.



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}

      Seconded on it being a quoting issue. In general when you've got something harry like this that's going to get passed through several shells it's best to stick the part that needs quoting in its own script on the remote side. Then you simply run that script rather than trying to divine how many different layers of backwhacks and quotes and double quotes (and tigers and bears oh my . . .) you need.

      (And you probably also should consider getting a new sysadmin to replace whomever's still got the r-services enabled on machines; but that's neither here nor there . . .)

Re: substituting a regular expression in a file
by Tanktalus (Canon) on Sep 06, 2006 at 16:45 UTC

    Rule #1 of using system: avoid the shell. Unless you can't. Here is a case of both.

    You can avoid the shell locally. Do so. You can't avoid the shell remotely, so we'll deal with that.

    Rule #3 (I think) of regexps: avoid leaning toothpick syndrome (LTS). Too many escapes can get confusing. Change your delimiter if you have to. In your case, you're just escaping everything - no need. (Funnily enough, you missed the extra \ on "\." in "command\.package" - but by avoiding the extra shell, we shouldn't need to worry about it. As much.)

    system('rsh', $sdpType, qq[perl -pi -e 's/^(command\.package)=(\S+)/"\$1=$version"/gei' + $ENV{JBOSS_HOME}/install/version.properties]);
    You may still need to double what few escapes we have for the remote shell. And may need to double again for the local perl. But the $1 shouldn't need extra escaping. That said, you probably also want to simplify this just a wee bit more:
    system('rsh', $sdpType, qq[perl -pi -e 's/(?<=^\Qcommand.package=\E)\S+/$version/i' $EN +V{JBOSS_HOME}/install/version.properties]);
    By using a zero-width positive look-behind assertion (see perlre, we can get rid of your eval modifier. By using \Q and \E (those \'s may need doubling), we no longer worry about special characters (admittedly, there's only one - but it's a useful habit to be in). And your g modifier was never really needed since we anchored at one end anyway.

    (Warning: untested code - use at own risk.)

      To check one's work, just go one step at a time.

      Inside of qq[...], \. is just . and \S is just S. So rsh will get the following argument:

      perl -pi -e 's/^(command.package)=(S+)/"$1=..."/gei' $ENV{JBOSS_HOME}/ +install/version.properties

      which is clearly wrong.

      I'd replace \. with [.] since that survives this type of situations more easily. So the remaining trick is how to get \S to the remote Perl. \\S would give \S to rsh which would give '...\S...' to the remote shell which would work.

      The /e isn't need on this s/// so drop that and drop the double quotes as well:

      system( 'rsh', $sdpType, q[perl -pi -e 's/^(command[.]package)=(\\S+)/$1=] . $version . q[/gi' $ENV{JBOSS_HOME}/install/version.properties] +, );

      Note that qq[\\S] eq q[\\S], and I avoid using things like q[\S] since it tends to lead people into thinking that q[\\mach\share\dir\] works.

      This sends the following argument to rsh:

      perl -pi -e 's/^(command[.]package)=(\S+)/$1=.../gi' $ENV{JBOSS_HOME}/ +install/version.properties

      which gets sent to the remote shell producing the following arguments

      perl -pi -e s/^(command[.]package)=(\S+)/$1=.../gi .../install/version.properties

      which should work.

      Note that if $version ends up containing any single quote characters, then you are in trouble again.

      Inserting the 'echo' command at different points can help you while you figure out how quoting works at the different stages:

      system( 'echo', 'rsh', $sdpType, q[perl -pi -e 's/^(command[.]package)=(\\S+)/$1=] . $version . q[/gi' $ENV{JBOSS_HOME}/install/version.properties] +, ); #then system( 'rsh', $sdpType, q[echo perl -pi -e 's/^(command[.]package)=(\\S+)/$1=] . $version . q[/gi' $ENV{JBOSS_HOME}/install/version.properties] +, );

      - tye        

Re: substituting a regular expression in a file
by szabgab (Priest) on Oct 17, 2006 at 06:46 UTC
    You can probably eliminate one or even two levels of escaping here if using a module that wraps rsh (or telnet or ssh). Net::Rsh], File::Remote Net::Telnet and Net::SSH came up in a quick search.

    Others have already reflected on how to improve the regex itself.