Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Aging Script

by Seema (Novice)
on Aug 08, 2001 at 22:11 UTC ( [id://103142]=perlquestion: print w/replies, xml ) Need Help??

Seema has asked for the wisdom of the Perl Monks concerning the following question:

I have an example code here. Though I need to create a code that needs to loop/terminate as soon as the creation_date is created the cutoff_date (less than 31days is terminated.)

Code I am trying to explain:
Cutoff_date = Todays_date - 31 For each file If creation_date < cutoff_date delete file End If statement End For each loop
Example code:
#!/sbin/sh touch -t `date +"%m%d%H%M.%S"` timefile sleep 5 #sleep 5 seconds touch timefile2 echo `find ./ -type f -newer timefile`
With the example code..instead of -newer, I want to replace it with -lesser (or some sort of less command here). Hopefully someone can help me out! :o)
~Seema

Replies are listed 'Best First'.
Re: Aging Script
by arturo (Vicar) on Aug 08, 2001 at 22:19 UTC

    Perl makes this sort of thing very easy to do. If you want to delete all files that are older than 31 days, you need to read up on the stat function -- specifically, subtract the "ctime" of the file from the current timestamp and see if it's greater than (24 * 60 * 60 * 31). Also, read up on unlink, and possibly File::Find.

    HTH.

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
      Hey arturo...
      I read the stat function, unlink, and File::Find. They really helped out. Thanx for helping me out!
      ~Seema
Re: Aging Script
by suaveant (Parson) on Aug 08, 2001 at 22:27 UTC
    try this...
    $cutoff = time() - 31*24*60*60; while(</dir/to/read/*>) { unlink $_ if (stat $_)[10] < $cutoff; }
    that should work... maybe replace unlink with print to test it first ;)

                    - Ant
                    - Some of my best work - Fish Dinner

      Suaveant..
      Thanx, that helped out. The script runs fine!
      ~Seema
Re: Aging Script
by OzzyOsbourne (Chaplain) on Aug 08, 2001 at 22:37 UTC

    I did something like this a while ago. Because I did't know about strict at the time, you will have to modify it slightly...Example

    -OzzyOsbourne

      I ran the script again. I am getting an error in the 'Process arguments(help, days).
      It says: Name "main::opt_h" used only once.
      For:
      if($opt_h){ die "\agecmd.pl -d[days to age]\n"; } if($opt_d){ if($opt_d gt 9){ die "Error: days option must be a number. Use agecmd.pl -d[days to a +ge]\n"; } ...
      Should the code be???
      if($opt_h){ die "\agecmd.pl -d[days to age]\n"; } if($opt_d){ if($opt_d gt 9){ die "Error: days option must be a number. Use agecmd.pl -d[days to a +ge]\n"; } ...
      ~Seema
      Thanx Ozzy...
      I checked out the script you wrote. I have taken a few things out...though the script runs just fine! Thanx again!
      ~Seema
      Hi Ozzy..
      I tried the example that you had given to me yesturday. I did have an error for the opt_h and opt_d.
      I replaced the opt_h command with the opt_d command, now the script runs just fine. Though I know this is not the way you have made the script. I know the way I have changed it, is wrong. Could you please let me know what I can do!?! I know I need the opt_h because its the help command and opt_d is the days command. Please let me know as soon as you can. Thanx for your help!
      ~Seema

        the 1st if statement here says if the user uses -h appended to the end of the script file, die an tell them how to run the command properly.

        the second part says: if it is not a number, die, otherwise set $agedays to $opt_d (the value of -d).

        Normally, I would run agecmd.pl -d90

        By switching the two, you have made the d option the help option, and made the -h option the number of days. By not specifying -d, $agedays is then set to 60 by default, giving you no problems.

        By taking things out, and changing the code, it's hard to know what happened. Does you have problems if you run the original code with no mods? Can you post your new code?

        if ($opt_h){ die "\agecmd.pl -d[days to age]\n"; } if ($opt_d){ if ($opt_d gt 9){ #tests for strings die "Error: days option must be a number. Use agecmd.pl -d[da +ys to age]\n"; }else{ $agedays=$opt_d; } }

        -OzzyOsbourne

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://103142]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found