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

Re: [Sheflug] shell script challenge



On Thu, Jul 24, 2003 at 05:16:59PM +0100, Simon Brown wrote:
> On 2003.07.24 17:21, Dave Mitchell wrote:
> >On Wed, Jul 23, 2003 at 11:32:17PM +0100, Simon Brown wrote:
> >> Hi,
> >>
> >> A quick and simple challenge for all the shell scripting gurus out
> >there.
> >
> >is Perl allowed?
> Yeah, if you think it's a better solution

Perl is generally best for this sort of thing.

below is a rough attmpt with some rudimentary error checking.
-- 
Justice is when you get what you deserve.
Law is when you get what you pay for.


use strict;
use warnings;

my $base_dir = '.';  # XXX
my $list_dir = "$base_dir/lists";
my $last_dir = "$base_dir/last_month";
my $archive_dir = "$base_dir/archive";


my @months = qw(janurary February March April May June July August
	September October November December);


my ($month, $year) = (localtime)[4,5];
$year += 1900;
$month = $months[$month];

my $archive_subdir = "$archive_dir/$year/$month";

opendir D, "$last_dir" or die "can't open $last_dir: $!\n";
my @files = grep /\@/, readdir(D);

if (@files) {
    mysys("mkdir -p $archive_subdir") unless -d $archive_subdir;
    foreach my $file ( [at] files) {
	my $afile = "$archive_subdir/$file";
	if (-f $afile) {
	    warn "$afile already exists!\n";
	    next;
	}
	mysys("mv $last_dir/$file $afile; gzip $afile");
    }
}

opendir D, "$list_dir" or die "can't open $list_dir: $!\n";
@files = grep /\@/, readdir(D);

foreach my $file ( [at] files) {
    my $lfile = "$last_dir/$file";
    if (-f $lfile) {
	warn "$lfile already exists!\n";
	next;
    }
    mysys("mv $list_dir/$file $lfile");
}


sub mysys {
    print " [at] _\n";
    ## XXX uncomment next line to make it actually work
    ##system @_;
}
___________________________________________________________________

Sheffield Linux User's Group -
http://www.sheflug.co.uk/mailfaq.html

  GNU the choice of a complete generation.