Operating System - HP-UX
1833760 Members
2190 Online
110063 Solutions
New Discussion

Help to write a shell script in HPUX

 
SOLVED
Go to solution
R.O.
Esteemed Contributor

Help to write a shell script in HPUX

Hi,

I am trying to write a program in shell script that does the following:

- check if three files (with its date of creation in its name) of today, yesterday and three days ago, exists
- If they exists, remove all the other files older than three days ago. If they do not
exists, do nothing.

I don
"When you look into an abyss, the abyss also looks into you"
12 REPLIES 12
R.O.
Esteemed Contributor

Re: Help to write a shell script in HPUX

I don´t know so much about shell script, so any help will be appreciated .

Regards,

(the messages was cu
"When you look into an abyss, the abyss also looks into you"
R.O.
Esteemed Contributor

Re: Help to write a shell script in HPUX

I don´t know so much about shell script, so any help will be appreciated .

Regards,

(the messages was cu
"When you look into an abyss, the abyss also looks into you"
Jannik
Honored Contributor

Re: Help to write a shell script in HPUX

If you assign points to all you old questions we might just help you!

company name:
country: spain
personal quote:
certification:
ITRC member since: April 21, 2003
last contribution date: February 03, 2005
I have assigned points to 46 of 134 responses to my questions.
jaton
Thierry Poels_1
Honored Contributor

Re: Help to write a shell script in HPUX

hi,

can you give examples of this filenames.

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
R.O.
Esteemed Contributor

Re: Help to write a shell script in HPUX

Hi,

Jannik : Sorry for the points not assigned; i use to forget to assign, but not in my lasts threads.

The names are in the form:

- filexxx--.Z

Regards,
"When you look into an abyss, the abyss also looks into you"
Peter Godron
Honored Contributor

Re: Help to write a shell script in HPUX

Hi,
if the file names are in the format *YYYYMMDD*
then somthing like this will get you started.

Some of the guys here have prewritten scripts that do this sort of thing. Use the keyword 'yesterday'in the forum search.
Regards
Thierry Poels_1
Honored Contributor

Re: Help to write a shell script in HPUX

hi,

as you still didn't give real examples, how about something general:

#! /usr/bin/ksh
nofiles=`find /yourdir -type f -name "file*.Z" -mtime -4 | wc -l`
if [ $nofiles -ge 4 ]
then
find /yourdir -type f -name "file*.Z" -mtime +4 -exec rm {} \;
fi



notes:
- adapt "name" and "-mtime" to your needs
- test this first without the "-exec rm {} \;"
- find will also work through subdirs, so don't use this if you have subdirs.

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
R.O.
Esteemed Contributor

Re: Help to write a shell script in HPUX

Hi,

The exactly names of the files are:

backup_01-02-05_00_00.dmp.Z , where "01-02-05_00_00" is "day-month-year_hour_minute"

I have already a script with "mtime" but I think it is a bit "dangerous". The script that have sent Peter, works fine to calculate the date; I only need to convert to this date and to remove he older...

Regards,

R.O.
"When you look into an abyss, the abyss also looks into you"
R.O.
Esteemed Contributor

Re: Help to write a shell script in HPUX

Hi,

Peter, how can I convert four digits of year in two? (tried printf("%02d... but it does not work)

In the other hand, how can I select the files older than the three firsts to delete them??

Regards,
"When you look into an abyss, the abyss also looks into you"
Thierry Poels_1
Honored Contributor

Re: Help to write a shell script in HPUX

hi,

1. something like:
two = `echo $four | cut -c3-4`

2.
find /yourdir ! -newer file3daysago ! -name file3daysago

or specify all 3 files:
find /yourdir ! -newer file1 ! -name file1 ! -newer file2 ! -name file2 ! -newer file3 ! -name file3

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
R.O.
Esteemed Contributor

Re: Help to write a shell script in HPUX

Hello,

I want to avoid to select files based upon in its modification time, but in the date inserted in its name. It is possible that someone modifies a file, so the modification time is not valid to do this.

Regards,
"When you look into an abyss, the abyss also looks into you"
Peter Godron
Honored Contributor
Solution

Re: Help to write a shell script in HPUX

R.O.,
to answer your question re 4/2 digits:

firstdate=`perl -e '($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time-2*86400); $fix_year = $year - 100; $fix_m
on = $mon + 1;printf("%02d%02d%02d\n",$fix_year,$fix_mon,$mday)'`

The difference is rather than adding 1900 you subtract 100 as year is number of years since 1900. The format can then be changed from %04 to %02. Problems MAY occurs with date before 1/1/2000, but this should not be problem for what you want to do.
Regards