Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi ! I am new to Unix, so i need ur help ... thanks

Status
Not open for further replies.

moretips

MIS
Sep 25, 2000
25
US
Hi ! I am new to Unix, so i need ur help ... thanks !

I use ksh shell script in HP-UX environment.
I have to write a script which do 3 things:

1. select some file from a directory
2. tar all these files into a file ( *.tar)
3. remove the file from the directory

The files which i look for are mails in Proc directory.
I have wrote the script but it didn't work, may i know what's wrong ?

#!/usr/bin/sh

AGE=1
DT='yesterday -$AGE | cut -c 4-5,1-2'
echo $DT

find ./m* -mtime +1 -print | tar -cvf mail.$(yesterday).tar | rm -f/mail/proc/m* [sig][/sig]
 
Try changing "mtime +1" to "mtime 1". This should look for files modified in the last 24 hours. If you enter "+1", I think find will look for files modified more than 24 hours ago.

By the way - nice trick with piping the output from tar into the rm command :) [sig]<p> Andy Bold<br><a href=mailto: > </a><br><a href= > </a><br>"I've probably made most of the mistakes already, so hopefully you won't have to..." Me, most days.[/sig]
 
Thanks Andy Bold! The script still cannot work. It said that:
yesterday -$AGE | cut -c 4-5,1-2
tar: cannot open mail.09/25/2000.tar.

May i know what's wrong ??? [sig][/sig]
 
It looks like you have a command called &quot;yesterday&quot; that returns a date from some time in the past. So, for example, your &quot;yesterday -$AGE&quot; gives you yesterdays date. (&quot;yesterday -1&quot;). The date it is returning is formatted using &quot;/&quot; separators. As this is the Unix directory separator your script is trying to create a file called &quot;2000.tar&quot; in a directory called &quot;mail.09/25&quot;. As tar will not create directories for you, the script fails.

There's a couple of possible ways around this.

If there an option to the &quot;yesterday&quot; command to use a different date separator, such as &quot;:&quot;, use that.

Alternatively, you could get the script to change &quot;/&quot; to &quot;:&quot;. I'm not sure what format &quot;yesterday&quot; returns the date in, but I'm assuming &quot;dd/mm/yyyy&quot;.
[tt]
#!/usr/bin/sh

AGE=1

# Remember to use backtics around the commands, and not
# single apostrophes.
DT=`yesterday -$AGE | cut -c 4-5,1-2`
DATESTAMP=`yesterday -$AGE | tr -d &quot;/&quot; &quot;:&quot;`

echo $DT

find ./m* -mtime +1 -print | tar -cvf mail.${DATESTAMP}.tar
rm -f /mail/proc/m*
[/tt]

Hope this helps. [sig]<p> Andy Bold<br><a href=mailto: > </a><br><a href= > </a><br>"I've probably made most of the mistakes already, so hopefully you won't have to..." Me, most days.[/sig]
 
Hi !
The files which i select are mails in Proc directory. The purpose of this script is to backup previous day's mail in a tar file, then remove the mails from Proc directory.

If i want to backup yesterday's mail, the AGE should be equal to 1, then it will search yesterday mails and tar it as mail.0925.tar. Finally the mails in the Proc directory will be remove.

I have yesterday scrip which can return yesterday date.The script is save as some where else in the machine.

The problem of my script is it cannot tar my file properly. Is the tar command cannot be included in a script ? Or is impossible to do these thing in a script
(tar files then remove files)??

Thanks for ur help !!!! :)


[sig][/sig]
 
OK... if &quot;yesterday&quot; just returns yesterday's date, then change the DATESTAMP line in my amended script to look like the following:
[tt]
DATESTAMP=`yesterday | tr -d &quot;/&quot; &quot;&quot;`
[/tt]

This will give you a date like &quot;09252000&quot;. This is almost what you are looking for - it just has the year added on to the end. I'd stick with this, because if you are still running this script the same time next year, then you are going to have two tar archives with the same name.

So, putting that into the full script gives us:
[tt]
#!/usr/bin/sh

AGE=1

# Remember to use backtics around the commands, and not
# single apostrophes.
DT=`yesterday -$AGE | cut -c 4-5,1-2`

# Use the &quot;yesterday&quot; command to return yesterday's date,
# and strip &quot;/&quot; characters out of the returned date.
DATESTAMP=`yesterday -$AGE | tr -d &quot;/&quot; &quot;&quot;`

echo $DT

# Find the requried files. Note that &quot;DATESTAMP&quot; uses the
# &quot;squiggly&quot; brackets {} and not normal brackets ().
# This is to make sure the shell doesn't try to look for
# &quot;$DATESTAMP.tar&quot; when expanding the environment variable.
find /Proc -name &quot;m*&quot; -mtime 1 -print | tar -cvf mail.${DATESTAMP}.tar

# Use &quot;find&quot; to do the file removal. More reliable than
# piping output into &quot;rm&quot;.
find /Proc -name &quot;m*&quot; -mtime 1 -exec rm -f {}

[/tt]

You may need to amend paths (&quot;/Proc&quot;, for example) in the script to reflect your actual system set up.

This should work OK. We do this kind of thing in several scripts on the systems here, and it works fine. [sig]<p> Andy Bold<br><a href=mailto: > </a><br><a href= > </a><br>"I've probably made most of the mistakes already, so hopefully you won't have to..." Me, most days.[/sig]
 
Thanks AndyBo !

The result of the script is :
09262000
Attempt to create archieve of no files. Nothing dumped.

In order for me to find out all the mails, does it mean that i have to write the full path of the mail (from the root)?

So to find the mail, it should :
find /das/cig2/stmadm/mail/proc, right ?

I think the tar command got problem, may be it could not be tar like that way ...... :(
[sig][/sig]
 
I noticed a problem running this find into tar via a pipe, I am still trying to work out why but the error is &quot;missing file names.
What does work is if I use the single back quote to make the find run then pass it as input to the tar, something like this: -

tar cvf test1.tar `find /mail/proc/m* -mtime 0 -print`

Notes. put the full path name after the find to ensure you are in the correct directory. Check the find command from the command line to see if it is actualy picking anything up. When you have 'find' working try passing the output to tar then when that works incorporate it into a script.
[sig]<p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>Top man[/sig]
 
thanks for all ur help !!!!!!!1

The script is work now. [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top