PERL script to read directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL script to read directory
# 1  
Old 07-23-2010
PERL script to read directory

Good day to all!

I'm currently doing a perl script about reading a directory.
What I'm going to do is write a script that will check whether the backup file is created daily or not inside the directory. If the backup file created, it will display "OK", else it will display the date of "no backup". If no data found, It will display "no backup found".

this is the file name once the backup file is created:

mysql07222010-18:00.gz


Thank you so much
# 2  
Old 07-23-2010
Quote:
Originally Posted by ellechim
...What I'm going to do is write a script that will check whether the backup file is created daily or not inside the directory. If the backup file created, it will display "OK", else it will display the date of "no backup". If no data found, It will display "no backup found".
this is the file name once the backup file is created:
mysql07222010-18:00.gz
...
You may want to use Perl's "-e" file test to check the existence of that file.

Code:
perl -le 'if (-e "/dir1/dir2/dir3/mysql07222010-18:00.gz"){print "OK"} else {print "No backup"}'

where "/dir1/dir2/dir3/" is the directory path of the gz file.

tyler_durden
# 3  
Old 07-23-2010
Thanks sir for the info.
my problem is the filename always change depends on the date it was created.
It performs daily backup and the file name change also:

mysql07222010-18:00.gz (mysql - folder name, 0722.. date of file when it was created)

as of now, i have no idea on how to display if there's a file created each day. Smilie
# 4  
Old 07-23-2010
Would something like this work for you?

Code:
#!/usr/bin/perl

chomp($part=`date +%m%d%Y`);
$fname="mysql$part-18:00.gz";

if (-e $fname)
{print "OK\n";}
else {print "No Backup\n";}

assuming your directory had:

Code:
-rw-r--r-- 1 opergfs prodsup   0 Jul 21 18:00 mysql07212010-18:00.gz
-rw-r--r-- 1 opergfs prodsup   0 Jul 22 18:00 mysql07222010-18:00.gz

then you would get "No Backup" as your output.

This would assume the script runs on the same day the backup file is generated.
# 5  
Old 07-23-2010
Code:
#!/usr/local/bin/perl

#$path = perl;

#use File::stat;
use Time::localtime;

sub ExampleFiles{
         $today = time;
         $today -= $today % 86400;
         $return_value = 0;
         $mtime = (stat("$_[0]"))[9] || die "cannot stat file $!";
         $size =  (stat("$_[0]"))[7];  # size in bytes$
         $ok = "NOT OK";
         my @a = glob "mysql*.gz" ;

         #my $d = ctime(stat($_)->mtime);

      if( $size > 0 && $mtime >= $today )
       {
          $ok = "Successful";
          $return_value = 1;
       }


#foreach (@a) {
#    if ( -e $_)
#    {
#          print "File found $_.\n";
#    }
#    else
#    {
#            print "No file found $_.\n";
#    }
#}



        print "$_[0] is $ok \n ";
        return $return_value;
}
$found = 0;

 opendir (DIRHNDL, "/Backup/servers/10.1.10.35") ||  die "Cannot open directory $!";
@filelist = readdir (DIRHNDL);

closedir (DIRHNDL);
foreach $filename ( @filelist )

{
  if ($filename =~ $path)
   {
     $found |= ExampleFiles $filename;
   }

foreach (@a) {
    if ( -e $_)
    {
          print "File found $_.\n";
    }
    else
    {
            print "No file found $_.\n";
    }
}

}

sir, i would like to know if the script is on the right track based on my concern.
Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl SCript to read file content (if else statemenet)

Hi All, I wanted to write a perl script to read the content in a file,the file content is either 0 (zero) OR 1. The idea is like this. If (content =1), then it will proceed to perform some step. and then update the file content to 0(zero) else if (content =0), it will update the content to... (11 Replies)
Discussion started by: hploh
11 Replies

2. Shell Programming and Scripting

Calling an interactive perl script from within a while-read loop

Hi, I have a perl script that prompts for a user to enter a password before doing what it does. This works well if I call it directly from a bash script #!/bin/bash /path/to/perl/script $arg1 $arg2 But, when I try to enclose this within a while read loop, the perl script is called but... (1 Reply)
Discussion started by: prafulnama
1 Replies

3. Shell Programming and Scripting

Need a UNIX/perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perl/unix script to read and write the data using perl script? Regards, Ravi (4 Replies)
Discussion started by: toravi.pentaho
4 Replies

4. Shell Programming and Scripting

Need a perl script to read and write the data

Hi, I have on Designdocument in that information is stored with in tabular format.I need Perlscript to read and write the datausing perl script? Regards, Ravi (0 Replies)
Discussion started by: toravi.pentaho
0 Replies

5. Shell Programming and Scripting

How to read a directory as parameter in shell script?

Hi All, I have written a shell script to compile an oracle R12 form and also a procedure is called from it. The procedure registers the form, function and creates a menu entry. I took directory as a parameter. Code is as below: #!/bin/ksh echo... (3 Replies)
Discussion started by: veena484
3 Replies

6. Shell Programming and Scripting

Perl Script to read partitions

Hi Guys! I'm currently doing a perl/bash script that will read all the partitions in the server. I'm trying to use df -h but it will just display the partitions. Your help is very much appreciated. Thank you. (0 Replies)
Discussion started by: ellechim
0 Replies

7. Shell Programming and Scripting

need shell or Perl script to read multiple input

I need shell 0r Perl script to read multiple input and do something and come out example: echo “ enter the host names separated by space “ read servers foreach @servers { do do something done} Here host names like host1 host2 host3 . . . . . . . so on Please help me... (8 Replies)
Discussion started by: sreedhargouda
8 Replies

8. Shell Programming and Scripting

perl script example to take password hiden and read from it

Hi, I am new to perl scripting Can you please let me know if you have an example on how to read the hidden password and read from it? Thanks, Mahesh (1 Reply)
Discussion started by: mum6ue
1 Replies

9. Shell Programming and Scripting

Perl script variable to read shell command

Solaris 10 Korn shell ksh, Hi there, I have figured out to get yesterday's date which is using the below command: TZ=GMT+24; date +%d-%b-%Y to get the format of 30-Sep-2008 and TZ=GMT+24; date +%Y%m%d to get the format of 20080930. I need this two format. In my perl script below I need... (4 Replies)
Discussion started by: bulkbiz
4 Replies

10. Shell Programming and Scripting

How to read email using mailx in shell script or perl

Hello, I am new to mailx and perl and I need help. I need create a shell script to read the mails on the SUN server, then parse the subject line and message body of each email to extract particular data so that I can pass these data fields in as application parameters to invoke a java... (4 Replies)
Discussion started by: jliharper
4 Replies
Login or Register to Ask a Question