From owner-freebsd-questions@FreeBSD.ORG Sun Apr 8 09:40:16 2007 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id E7D7916A400 for ; Sun, 8 Apr 2007 09:40:16 +0000 (UTC) (envelope-from oregnier@steelbox.org) Received: from 30.mail-out.ovh.net (30.mail-out.ovh.net [213.186.62.213]) by mx1.freebsd.org (Postfix) with SMTP id 6216A13C4B0 for ; Sun, 8 Apr 2007 09:40:16 +0000 (UTC) (envelope-from oregnier@steelbox.org) Received: (qmail 3031 invoked by uid 503); 8 Apr 2007 09:40:32 -0000 Received: (QMFILT: 1.0); 08 Apr 2007 09:40:32 -0000 Received: from b7.ovh.net (HELO mail245.ha.ovh.net) (213.186.33.57) by 30.mail-out.ovh.net with SMTP; 8 Apr 2007 09:40:32 -0000 Received: from b0.ovh.net (HELO queue-out) (213.186.33.50) by b0.ovh.net with SMTP; 8 Apr 2007 09:40:13 -0000 Received: from mac76-2-82-241-6-173.fbx.proxad.net (HELO ?192.168.1.2?) (postmaster@steelbox.org@82.241.6.173) by ns0.ovh.net with SMTP; 8 Apr 2007 09:40:11 -0000 Message-ID: <4618B887.4030709@steelbox.org> Date: Sun, 08 Apr 2007 11:40:23 +0200 From: Olivier Regnier User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 To: Giorgos Keramidas References: <4617B954.7010507@steelbox.org> <20070407171120.GA70957@kobe.laptop> In-Reply-To: <20070407171120.GA70957@kobe.laptop> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Ovh-Remote: 82.241.6.173 (mac76-2-82-241-6-173.fbx.proxad.net) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|H 0.5/N Cc: freebsd-questions@freebsd.org Subject: Re: script perl with sed command X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 08 Apr 2007 09:40:17 -0000 Giorgos Keramidas a écrit : > On 2007-04-07 17:31, Olivier Regnier wrote: > >> Hello, >> I have a problem with my perl script with the command sed. Here is a >> example of my code: >> > > Don't use system("sed ...") in Perl. It's considered poor style, since > Perl can do the same without having to fork a shell/sed process. > > >> # Selecting the fast server >> print "Using the server called $server"; >> system(`/usr/bin/sed 's|\*default host=\(.*\)|\*default host=${server}|' >> $standard_supfile > $standard_supfile.copy`); >> system('/bin/mv $standard_supfile.copy $standard_supfile'); >> > > Try using Perl only, instead of forking sed(1), like this: > > ,----------------------------------------------------------------------- > | > | #!/usr/bin/perl -Tw > | > | use strict; > | > | # > | # supfile_set_default_host($supfile, $newhost) > | # Set the default host used by the supfile $supfile to the > | # host name supplied as $newhost. > | # > | > | sub supfile_set_default_host($$); > | sub supfile_set_default_host($$) > | { > | my $tmpsupfile; > | my $supfile = shift; > | my $newhost = shift; > | > | if (!defined($supfile) || !defined($newhost)) { > | return undef; > | } > | > | $tmpsupfile = "tmp-" . $supfile; > | open(SUP, "$supfile") or die "$!"; > | open(TMP, "> $tmpsupfile") or die "$!"; > | > | my $line; > | while (defined($line = )) { > | chomp $line; > | $line =~ s/^(\*[ \t]*default[ \t][ \t]*host[ \t]*=).*/$1${newhost}/; > | print TMP "$line\n"; > | } > | close(TMP) or die "$!"; > | close(SUP) or die "$!"; > | rename("$tmpsupfile", "$supfile") or die "$!"; > | return 1; > | } > | > | supfile_set_default_host('standard-supfile', 'cvsup.example.net'); > | > `----------------------------------------------------------------------- > > This is slightly more complex than forking a sed(1) utility run, but > it's easier to understand (at least it is for me). > > A very brief run of the script seems to work here: > > ,----------------------------------------------------------------------- > | > | $ pwd > | /tmp > | $ cp /usr/share/examples/cvsup/standard-supfile . > | $ grep 'default host' standard-supfile > | *default host=CHANGE_THIS.FreeBSD.org > | $ perl -Tw supfile.pl > | $ grep 'default host' standard-supfile > | *default host=cvsup.keramida > | $ > | > `----------------------------------------------------------------------- > > - Giorgos > > Hello and thanks for this perl script. I'm new in perl and when i test him, i have an error that says: No such file or directory at myscript.pl line 18 line 18 = open(TMP, "> $tmpsupfile") or die "$!"; #!/usr/bin/perl -Tw use strict; # # supfile_set_default_host($supfile, $newhost) # Set the default host used by the supfile $supfile to the # host name supplied as $newhost. # sub supfile_set_default_host($$); sub supfile_set_default_host($$) { my $tmpsupfile; my $supfile = "/etc/standard-supfile"; my $newhost = "cvsup.fr.freebsd.org"; if (!defined($supfile) || !defined($newhost)) { return undef; } $tmpsupfile = "tmp-" . $supfile; open(SUP, "$supfile") or die "$!"; open(TMP, "> $tmpsupfile") or die "$!"; my $line; while (defined($line = )) { chomp $line; $line =~ s/^(\*[ \t]*default[ \t][ \t]*host[ \t]*=).*/$1${newhost}/; print TMP "$line\n"; } close(TMP) or die "$!"; close(SUP) or die "$!"; rename("$tmpsupfile", "$supfile") or die "$!"; return 1; } supfile_set_default_host('standard-supfile', 'cvsup.example.net'); Thanks again for your help.