[Chicago-talk] Q on wildcards in output options..

Andy_Bach at wiwb.uscourts.gov Andy_Bach at wiwb.uscourts.gov
Mon Oct 13 18:15:16 CDT 2003


One idea, don't let them use the '*' in the output switch, do something 
like let them give  you a output log file or an output log dir and an 
extension (w/ maybe and append/overwrite flag) perhaps:
use Getopts::Std;
use vars qw(%opts);
my $default_log_dir = "/tmp";
my $default_log_extension = "clean.logs";
my $Usage = "Usage: $0 [-o <output file>|-O <output dir> [-x <output file 
extension>] ] <log dir
   where
     -o  <output file>   : file to send output to.
or: 
    -O  <output dir>    : dir to send output to, one per input logs
    -x    <extension>  :  file extension for output logs, 
                                           default is: 
$default_log_extension
   e.g.
   $0 -O /tmp/cleanlog -x $default_log_extension   /var/log
(where /var/log contains:
syslog
sendmail.info
)  gets you:
  /tmp/cleanlog/syslog_clean.logs
   /tmp/cleanlog/sendmail.info_clean.logs
";

getopts('O:x:o:', \%opts) or die "$Usage
die "Either -O or -o, no both\n$Usage"
     if $opts{O} and $opts{o};
die "-x is only allowed w/ -O\n$Usage"
  if $opts{o} and $opts{x};

my ($output_log_dir, $output_log_extension);
if ( $opts{O} ) {
   die "-O must be a dir\n$Usage"
      unless -d $opts{O};
   $output_log_extension = $opts{x}
         if $opts{x};
} else {
   $output_log_dir = $default_log_dir;
}       # if $opts{O}
$output_log_extension = $default_log_extension
     unless $output_log_extension;

my $open_output_log = 1;
while (<>) {
    if ( $open_output_log ) {
       my $log_to_clean = $ARGV;
       $olog = $output_log_dir . "/" . $log_to_clean . "_" 
$output_log_extension;
        open(OLOG, ">$olog " ) 
            or die "can't open output log: $olog: $!";
       $open_output_log = 0;
    }     # if open_output_log

   # clean log, output to OLOG handle

 } continue {
      if ( eof)  {     # Not eof()!
          close OLOG;
          open_output_log++;
       }      # if eof
 }      # while <>


untested, esp. that log open trick (perldoc -f eof) but it should work.

a

Andy Bach, Sys. Mangler
Internet: andy_bach at wiwb.uscourts.gov 
VOICE: (608) 261-5738  FAX 264-5030

"We are either doing something, or we are not. 'Talking about' is a 
subset of 'not'."            -- Mike Sphar in alt.sysadmin.recovery



More information about the Chicago-talk mailing list