in reply to Reading from STDIN
I know you didn't ask, but some of the code used here is not very secure. Other parts can be written better (if you plan to make the script portable)
Here is an updated version:
#!/usr/bin/perl use strict; # amen use warnings 'all'; # or use perl -w use File::Basename; $| = 1; # :raw for brain-dead OSes (or use binmode). open my $in, '<:raw', $ARGV[0]; # use $in not IN open my $out, '>:raw', "differences"; while (<$in>) { chomp; ($file,$problem,@machines) = split(/,/, $_); ($name,$path,$suffix) = fileparse($file,""); print "$name -- $path -- $suffix\n"; if($problem eq "different") { # get the file from the remote machine foreach $machine (@machines) { next if ($machine eq ""); # consider using File::Temp here $tempfile = "/tmp/" . $name . "_" . $machine; # should consider using File::Remote here system("rcp", "$machine:$file", $tempfile) == 0 or die "Execution (rcp) failed: $?"; # why not use Text::Diff ? print "comparing $name from $machine\n"; system("diff", $file, $tempfile) == 0 or die "Execution (diff) failed: $?"; print "(k)eep or (i)gnore this difference?\n"; $resp = <STDIN>; print ">>$resp<<\n"; if($resp eq "k") { print $out "$machine $file\n"; } } } }
Executing stuff is always insecure. You should either consider using taint mode (-T) or use Perl modules to perform the same task in most cases (in all cases in the example above)
Documents for these modules are here: File::Remote, File::Temp and Text::Diff. They are more or less simple to use, a lot safer and sometimes faster. (since they don't fork() and exec())
Update: Use chomp instead of chop.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Reading from STDIN
by unstable_geek (Acolyte) on Feb 06, 2004 at 18:55 UTC |