Visit Our UNIX and Linux User Community


help with if statement perl scripting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with if statement perl scripting
# 1  
Old 11-05-2009
help with if statement perl scripting

Sorry for the newbie question

I need to create a perl script to check the modifed time of a file I already have that part created but need help in creating a if statement that basicly will check to see if the modifed time is at least 2hrs older then the current time if the stament is true to send out a email

here is what I have
the print statement are mainly just for debugging purpose
Code:
#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
# To get time
my $currDate = time;
my $thisFile = "/usr/local/ahps2/tmp/****";
my $mtime  = (stat($thisFile))[9];
print "1: $mtime \n";
my @lt = localtime($mtime);
# The get file info
my $ptime = strftime ("%R",@lt);
print "2: $ptime \n";
print "3: $currDate \n";
print "\n";
print $currDate . "\n";


Last edited by pludi; 11-06-2009 at 02:31 AM.. Reason: code tags, please...
# 2  
Old 11-05-2009
Quote:
Originally Posted by nettech207
...
if statement that basicly will check to see if the modifed time is at least 2hrs older then the current time if the stament is true to send out a email
...
Since "time" returns the number of non-leap seconds since whatever time your system considers to be The Epoch, you simply need to check that the difference between current time and the file's mtime is at least 7200.

Here's one way to do it:

Code:
$
$ cat -n cmpmtime.pl
     1  #!/usr/bin/perl -w
     2  $file = $ARGV[0];
     3  if (time - (stat($file))[9] >= 7200) {
     4    print "The file $file is at least 2 hours old.\n";
     5  } else {
     6    print "The file $file is less than 2 hours old.\n";
     7  }
     8
$
$ # I have a file already (tstfile) that is more than 2 hours old
$
$ perl cmpmtime.pl tstfile
The file tstfile is at least 2 hours old.
$
$ # And I shall create a file right now
$ touch newfile
$
$ perl cmpmtime.pl newfile
The file newfile is less than 2 hours old.
$
$

HTH,
tyler_durden
# 3  
Old 11-06-2009
thanks durden that helps alot now to figure oout how to sendmail if statement is true

thanks again

Previous Thread | Next Thread
Test Your Knowledge in Computers #805
Difficulty: Medium
The goal of a Digital Signal Processor (DSP) is usually to measure, filter or compress discrete digital signals.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting with case statement

Foe example we have three environments int,qa and prod.Each environment has some number of servers. int=Server1,Server2,Server3 qa=Server4,Server5,Server6 prod=Server7,Server8,Server9 echo "Enter the Environment i.e int,qa,prod" read env case $env in int) ## Need command where all the... (9 Replies)
Discussion started by: nareshreddy443
9 Replies

2. Shell Programming and Scripting

Generate sql statement using shell scripting

Can anyone please assist me? I have attached 2 input files and one output file. I need to generate the sql update statements using the above 2 input files. if inputfile2 has 5 rows, then we should generate 5 update statements because column1 is unique. inputfile1 and inputfile2 may contain more... (10 Replies)
Discussion started by: vinus
10 Replies

3. Shell Programming and Scripting

Menu and case statement scripting

hi all i am trying to get help with writing a script using case statement to display menu as 1) Authentication log 2) System log 3) Messages 4) Dmesg 5) Boot log Q) Exit When selecting the menu by 1 or 2 or 3 o 4 or 5, it should display the last 10 lines of the log files, if... (3 Replies)
Discussion started by: renegade11
3 Replies

4. UNIX for Dummies Questions & Answers

BASH Shell Scripting: If, Then Statement

I'm having trouble trying to create a BASH shell script. I want the user to input a command "cat file_name.c" and then the shell script will delete all comments "/* */" from file_name.c else exit. So far I have this: #!/bin/bash read "cat file" // User will input command cat... (7 Replies)
Discussion started by: inkjoy00
7 Replies

5. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

6. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

7. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

8. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

9. UNIX for Dummies Questions & Answers

case statement in UNIX scripting (ksh)

Hi, I have a script like below : #!/bin/ksh echo "Do you want to export all docs ?" read alld echo "Do you want to export template or report only " read temr case && ] #arguments ;; case && ] #arguments ;; case && ] #arguments ;; (4 Replies)
Discussion started by: luna_soleil
4 Replies

10. Shell Programming and Scripting

perl if statement

Hello guys, I was wondering why my code doesn't read a variable when using if statement as follows: $userlist='users.txt'; $user='b999'; open (ACTULOG, ">>$userlist"); print ACTULOG "$user\n"; close (ACTULOG) this will work and prints... (3 Replies)
Discussion started by: Bashar
3 Replies

Featured Tech Videos