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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

csh shell script to cleanup log files

Status
Not open for further replies.

homBase

Technical User
Apr 1, 2002
12
US
Hi all,

Can anyone assit me?

I wrote this script to clean up log files located in a directory called out. The script creates a directory called TEMP and moves the logs into the directory then tars the directory and compresses it. After all that is done it will remove the temporay directory.

My problem is that when I tested the script without any log files it bombs out at the first foreach that searches for log files.

The complaint that the script gives is as follows:

foreach: No Match

I understand that it is not finding any *.log files but it should continue through the other conditions. The script will not complain if there isn't a TEMP directory.

Here is a copy of my script.


#!/bin/csh

while(1)


echo " starting clean-up in 10 seconds "
sleep 10

envout

foreach log (*.log)

if(-f $log && -d TEMP)then
tar cfv temp.tar TEMP;
compress temp.tar;
rm TEMP/*.*;
mv $log TEMP;

else if(-f $log)then
mkdir TEMP;
mv $log TEMP;
#tar cfv temp.tar TEMP;
#rm -R TEMP;

else if(-d TEMP)then
tar cfv temp.tar TEMP;
rm -R TEMP;
endif
end

foreach file (temp.tar)

if(-f $file && -d TEMP)then
@ $file++
tar cfv $file TEMP;
compress $file;
rm -R TEMP;

else
echo "There are no Log files or a TEMP directory"

endif

end

echo " Finished in 10 seconds"
sleep 10

end #I will not include this so it will only run once.


 
Why not just create a dummy log file at the start of your script so that the situation doesn't occur? HTH.
 
Ken I could do that, but it turn a false condition into a true condition.

 
homBase:

I'm not a csh person, and I don't mean to elaborate on the obvious, but why don't you check if you have any files before executing the foreach:

ls *.log

Regards,

Ed
 
Ed

That is what the if statment does, it test with a condition ie.. if(-f $log && -d TEMP)then, and if the condition is true the following arguments will be executed.

What it shouldn't do is bum out if part of the condition is false, it should test the 2nd condition and so on until it meets a true condition.
 
sounds like an '||' to me......... vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
ALCON,

What I did is applied Kencunningham's logic and created a dummy log.

I guess I am at the point of saying that this is a problem within csh scripting. I will probally rewrite this in ksh.
 
Thanks Ken and everyone else for your help, time and knowledge.

Thank you!

hombase
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top