Chris Nehren on 19 Apr 2011 07:40:08 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] perl -e with system()


On Apr 19, 2011, at 10:28 , Randall A Sindlinger wrote:

> Hey folks,
> 
> I'm getting a weird error.
> 
> I have a tab-separated datafile of the form
> 
> audit-2.0.5-5.8.1.x86_64	/var/log/audit/audit.log
> ca-certificates-1-9.1.noarch	/var/lib/ca-certificates/ca-bundle.pem
> filesystem-11.4-11.14.1.x86_64	parent is /boot/	/boot/backup_mbr
> filesystem-11.4-11.14.1.x86_64	parent is /etc/	/etc/.pwd.lock
> 
> I want to split the filenames up into separate files, just using the package
> as the filename.
> 
> perl -n  -e '($a, $b, $c) = split("\t"); chomp $a; chomp $b; chomp $c; if ($c) {system ("echo $c >> $a")} 
> else {system ("echo $b >> $a")}' < data.file

The problem is that you're not quoting things properly. The LIST form of system does this for you, but it also doesn't invoke a subshell (so you can't do >>). You really should be using open instead:

open my $fh, '>>', $file or die "open($file): $!";
print $fh $content;
close $fh;

Note also that $a and $b are special variables used by perl for sort() and you shouldn't use them. See perldoc perlopentut for more info, and please consider reading Beginning Perl so you don't make a mess of things.

> Thanks,
>         . o 0 ( So _this_ is why I should've gone to JP's talk )

This has very little to do with the shell and everything with perl.


-- 
Thanks and best regards,
Chris Nehren

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug