From owner-freebsd-questions@FreeBSD.ORG Sun Apr 8 16:28:20 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 7B27A16A400 for ; Sun, 8 Apr 2007 16:28:20 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from mxout1.cac.washington.edu (mxout1.cac.washington.edu [140.142.32.134]) by mx1.freebsd.org (Postfix) with ESMTP id 5856E13C45B for ; Sun, 8 Apr 2007 16:28:20 +0000 (UTC) (envelope-from youshi10@u.washington.edu) Received: from smtp.washington.edu (smtp.washington.edu [140.142.33.9] (may be forged)) by mxout1.cac.washington.edu (8.13.7+UW06.06/8.13.7+UW07.03) with ESMTP id l38GSJbf016158 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sun, 8 Apr 2007 09:28:19 -0700 X-Auth-Received: from [192.168.10.45] (c-24-7-142-221.hsd1.ca.comcast.net [24.7.142.221]) (authenticated authid=youshi10) by smtp.washington.edu (8.13.7+UW06.06/8.13.7+UW07.03) with ESMTP id l38GSESH023382 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Sun, 8 Apr 2007 09:28:17 -0700 Message-ID: <46191814.5080807@u.washington.edu> Date: Sun, 08 Apr 2007 09:28:04 -0700 From: Garrett Cooper User-Agent: Thunderbird 1.5.0.10 (X11/20070325) MIME-Version: 1.0 To: freebsd-questions@freebsd.org References: <4617B954.7010507@steelbox.org> <20070407171120.GA70957@kobe.laptop> <4618B887.4030709@steelbox.org> In-Reply-To: <4618B887.4030709@steelbox.org> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-PMX-Version: 5.3.0.289146, Antispam-Engine: 2.5.0.283055, Antispam-Data: 2007.4.8.91535 X-Uwash-Spam: Gauge=IIIIIII, Probability=7%, Report='__CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __HAS_MSGID 0, __MIME_TEXT_ONLY 0, __MIME_VERSION 0, __SANE_MSGID 0, __STOCK_PHRASE_7 0, __USER_AGENT 0' 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 16:28:20 -0000 Olivier Regnier wrote: > 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. The file has to exist that you're trying to modify, otherwise it'll give up :). Permissions issue? Better to do that section may be: my $tmpsupfile; my $supfile = "/etc/standard-supfile"; my $newhost = "cvsup.fr.freebsd.org"; if (!defined($supfile) || !defined($newhost)) { return undef; } $tmpsupfile = new File::Temp(); die "$!" unless(defined($tmpsupfile); open(SUP, "$supfile") or die "$!"; You need to add: use File::Temp; to the top of your script after "use strict;" though, and the Perl module *should* be available with standard installations. Cheers, -Garrett