Please check perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please check perl script
# 1  
Old 07-23-2010
Please check perl script

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";
    }
}

}

guys, please check my code above. I'm trying to 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".

Is my code on the right track?

Thanks for the help
# 2  
Old 07-23-2010
well, if it works it's correct.
have you tried it?

At least do some of your homework yourself.
# 3  
Old 07-23-2010
Billy is right - we do have a special homework forum. This does not appear too homework-like to me.

However, if this is sysadmin production code, a more normal approach is shell
Code:
found=$(find /Backup/servers/10.1.10.35 -mtime -1 -size +0 -name 'mysql*.gz')
[[ ! -z "$found" ]] && echo 'backup ok'  || echo 'backup failed'

which would you prefer to maintain? 25 lines of perl or 2 lines of shell....

Meant to add this http://perldoc.perl.org/File/Find.html and forgot. Always check perldoc, don't reinvent the wheel, you get get a flat tire instead.

Last edited by jim mcnamara; 07-23-2010 at 11:40 PM..
# 4  
Old 07-24-2010
thanks guys
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell/perl script to check for files

Hi, I am trying to write a script for following scenario: I have a list of countries from where I receive files...eg. (Indonesia, Thailand, Australia...etc) For each country, I have a list of files that they send. IND -- a,b,c TH -- p,q,r AU -- x,y,z The path for these files could... (2 Replies)
Discussion started by: neil.k
2 Replies

2. Shell Programming and Scripting

How to check if printer is out of paper using perl script ?

Hello, I need to chack if the printer is out of paper, and send message to operator. I need to do this from perl script. The printer have mechanism to check if it have paper. However, the cups does not report "printer out of paper" when I remove the paper, and try to print. Is there any... (1 Reply)
Discussion started by: +Yan
1 Replies

3. Shell Programming and Scripting

Help with Perl script that can check a URL and notifiy when changes occur

I'm a scripting newbie and I'm trying to learn. No better way than being assigned a project. So basically, I'm trying to come up with a script that can periodically check a URL and then notify when changes occur to the file. So what I'm thinking is that I need to devise a PERL script that... (3 Replies)
Discussion started by: adam1mc
3 Replies

4. Shell Programming and Scripting

perl script to check the mail ids in the correct format or not

Hi Folks, I have few mailids in a text file and need to check whether the mailid is in correct format or not. If just to check whether the string is a mailid or not there is a perl module Email::Valid to do the business or we can implement our own logic. But the mail_ids I am having is... (4 Replies)
Discussion started by: giridhar276
4 Replies

5. Shell Programming and Scripting

Perl script to check uname options

I am trying to read the /etc/security/limits.conf file to match with my specifications. Specification should match below : nofile=131072 noproc=131072 memlock=3500000 I have written a perl script to read the file: #!/usr/bin/perl $FILE1 = "/home/sriram/perl_scripts/limits.conf";... (2 Replies)
Discussion started by: sriram003
2 Replies

6. Shell Programming and Scripting

perl check email script not seeing attachment

The following script does pull the sender and Subject of the email but it is not seeing the attachment name. I know there is an attachment. I line in red SHOULD pull the filename out. this line is in the message: Content-Disposition: attachment; filename="Picture 243.jpg" ... (1 Reply)
Discussion started by: Ikon
1 Replies

7. Shell Programming and Scripting

Perl Variable Check Script

I am working on a perl script that is used to update a list of hosts to a certain file but I am having an issue when I try to perform a check to make sure the user enters valid information. The following is what I have currently written for the script: IPINPUT: print "Enter IP Address: ";... (2 Replies)
Discussion started by: Takau
2 Replies

8. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

9. Shell Programming and Scripting

Perl Script to check file date and size

Hi guys, i am new to perl. I started reading the perl documents and try to come up with some logic. I am trying to create a script that would go into a location, search for todays files, then searches for all .txt files from today. If todays not found, its an error If file size is less... (26 Replies)
Discussion started by: DallasT
26 Replies

10. Shell Programming and Scripting

Perl script to check free disk space

hello, I have to check the free space on the disk that would work both on Windows and Unix platform e.g on C: \ for Windows and / on Unix. I could use Unix command 'df ' ( my windows system has Unix emulator cygwin and could run 'df ' as well). But I'd like not to rely on system command but... (1 Reply)
Discussion started by: susja
1 Replies
Login or Register to Ask a Question