Tony Heal wrote:

> OK I am probably missing something stupid, but I can not get this to work.
> The output should be 'Daily-{day of week)-{MMM}-{DD}-{YYYY}' for Sunday
> thru Friday and 'Weekly-{1|2|3}-{day of week)-{MMM}-{DD}-{YYYY} for
> Saturday and every fourth Saturday should start rotating months
> 'Month-{1|2|3}-{day of week)-{MMM}-{DD}-{YYYY}
> 
> Anyone got any ideas?
> 
> Oh, there is some code at the front that changes the system date from May
> 1 to 31



[ Whitespace adjusted to aid legibility. ]

> #!/usr/bin/perl
> use warnings;
> use strict;
> my $today = `date +%m%d%H%M`;

Why call an external program to do something that you can do in perl:

my @today = ( localtime )[ 4, 3, 2, 1 ];
$today[ 0 ]++;
my $today = sprintf '%02d%02d%02d%02d', @today;

Or:

use POSIX 'strftime';
my $today = strftime '%m%d%H%M', localtime;


> my $count = "501";
> while ( $count lt 532 ) {
>   my $that = "0$count" . "2100";

Do you want to use strings or numbers?

For strings:

for ( '0501' .. '0531' ) {
  my $that = $_ . '2100';

For numbers:

for ( 501 .. 531 ) {
  my $that = sprintf '%04d2100', $_;


>   system("date $that");

The date command changes the *system* date!  In other words, this change
will effect all other programs running on your system.  Do you really want
to do that?


>   my $backupBaseDir = '/gfsbackup';
>   my $logFile = "$backupBaseDir\/gfs_backup.log";

You don't have to escape the slash in a double quoted string.

>   my $scpCMD = 'scp -i /usr/backup/.ssh/haddock';

And here you have a double quoted string without escaped slashes.

>   my $backupDir = gfsBackup();
>   my @ePaceServers = ("silverfish", "catfish", "bonefish");
>   if ( ! -d $backupBaseDir )
>   {
>     mkdir $backupBaseDir;
>   }
>   sub gfsBackup

Subroutines in perl are package variables so there is no point defining them
inside a loop.


>   {
>     my ( $sec, $min, $hour, $mday, $mon, $yearOffset, $wday, $yday,
$isdst ) = localtime( time );
>     my $year = $yearOffset + 1900;
>     my ( $weeksec, $weekmin, $weekhour, $weekmday, $weekmon,
$weekyearOffset, $weekwday, $weekyday, $weekisdst ) = localtime( time - (
84600 * 8 ) );
>     my $weekyear = $weekyearOffset + 1900;
>     my ( $msec, $mmin, $mhour, $mmday, $mmon, $myearOffset, $mwday,
$myday, $misdst ) = localtime( time - ( 84600 * 29 ) );
>     my $myear = $yearOffset + 1900;

Shouldn't that be:

    my $myear = $myearOffset + 1900;

>     my $week = int( $yday / 7 );
>     my $sevendays = ( time - ( 84600 * 7 ) );
>     my @days =
( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" 
);
>     my @months =
( "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", 
"Dec" );
>     my ( $directory, @dir, $month, $gfsDir, $DatedgfsDir, $monthDirectory,
@monthDir, $lastWeek, $dirtemp );

The only variable you need to declare at this level is:

    my $gfsDir;

>     if ( $days[$wday] =~ "Saturday" )

Why not more simply:

    if ( $wday == 6 )  # 6 = Saturday

>     {
>       opendir( DIR, $backupBaseDir );

You should *always* verify that the directory opened correctly:

      opendir DIR, $backupBaseDir or die "Cannot open '$backupBaseDir' $!";


>       my @readdir = readdir( DIR );
>       closedir( DIR );
>       while ( <@readdir> )

That is the same as:

      while ( glob "@readdir" )

Which means that you are globbing the file names that you just got from
readdir so you are now reading the directory contents a second time.  This
will *not* work correctly if any of the file names contain whitespace or
glob meta-characters.

What you want is:

      for my $directory ( @readdir )


>       {
>         $directory = $_;
>         print "directory = $directory\n";
>         if ( $directory =~ "$months[$weekmon]-$weekmday-$weekyear" )
>         {
>           @dir = split( /-/, $directory );

          my @dir = split( /-/, $directory );


>           if ( $dir[0] =~ "Monthly" )

Shouldn't that be:

          if ( $dir[0] eq 'Monthly' )


>           {
>             $gfsDir = "Weekly-1";
>           }
>           if ( $dir[0] =~ "Weekly" )

Shouldn't that be:

          if ( $dir[0] eq 'Weekly' )


>           {
>             if ( $dir[1] lt 3 )

            if ( $dir[1] < 3 )


>             {
>               my $weekcount = $dir[ 1 ] + 1;
>               $gfsDir = "Weekly-$weekcount";
>             }
>             if ( $dir[1] eq 3 )

            if ( $dir[1] == 3 )


>             {
>               my $monthcount = `ls $backupBaseDir |
grep "$months[$mmon]-$mmday-$myear" | cut -d- -f2`;

No need to call three external programs to do what you can do in perl:

              my ( $monthcount ) = map +( split /-/ )[ 1 ],
<$backupBaseDir/*$months[$mmon]-$mmday-$myear*>;


>               if ( $monthcount eq "13" )

This will *never* match!  If the backquotes return '13' then $monthcount
will contain "13\n" and it will not match '13'.


>               {
>                 $gfsDir = "Monthly-1";
>               }
>               else
>               {
>                 $monthcount++;
>                 $gfsDir = "Monthly-$monthcount";
>               }
>             }
>           }
>           last;
>         }
>       }
>     }
>     elsif ( $days[$wday] =~ "Saturday" )

The if clause for this elsif is:

      if ( $days[$wday] =~ "Saturday" )

Which is *exactly* the same so this elsif block will *never* execute.


>     {
>       $gfsDir = "Weekly-1";
>       last;

At this point you are not inside a loop so using last is an error.


>     }
>     if ( ! $gfsDir )
>     {
>       $gfsDir = "Daily-$days[$wday]-$months[$mon]-$mday-$year";
>       mkdir "$backupBaseDir/$gfsDir";
>       chdir "$backupBaseDir/$gfsDir";
>       return $gfsDir;
>     }
>     else
>     {
>       $DatedgfsDir = "$gfsDir-$days[$wday]-$months[$mon]-$mday-$year";
>       mkdir "$backupBaseDir/$DatedgfsDir";
>       chdir "$backupBaseDir/$DatedgfsDir";
>       $gfsDir = $DatedgfsDir;
>       return $gfsDir;
>     }

You can remove a lot of duplicate code like this:

    $gfsDir = ( $gfsDir ?
$gfsDir : 'Daily' ) . "-$days[$wday]-$months[$mon]-$mday-$year";
    mkdir "$backupBaseDir/$gfsDir" or die "Cannot
mkdir '$backupBaseDir/$gfsDir' $!";
    chdir "$backupBaseDir/$gfsDir" or die "Cannot
chdir '$backupBaseDir/$gfsDir' $!";
    return $gfsDir;


>   }
> $count++;}



John

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to