Solved

UNIX BASH Script to Clear Log Files

Posted on 2010-11-23
7
931 Views
Last Modified: 2012-05-10

See code below, when I run this through a script I get an error on line 30 stating '>' is not expected. However if I run the same find command from the command line there is no error and it works fine.

Why is this? Any comments welcomed
#/usr/bin/ksh

#cd /bea_domains/config

#Find core files and remove
#find ./ -name "core" -type f -exec rm {} \;

#Find nohup files, check if open and remove if not or leave if is
#find ./ -name "*.nohup" -type f | while read file ; do lsof $file >/dev/null && >$file; done

#Set array variables
logDir[1]="/home/T104AHE"
logDir[2]="/bea_domains/config/AHE_CIT_PREPROD/logs"
logDir[3]="/bea_domains/config/AHE_CIT/logs"
logDir[4]="/bea_domains/config/AHE_Booking/logs"
logDir[5]="/bea_domains/config/AHE_CONSTAT/logs"

#Create loop to cycle through log directories and remove unnecessary files
for i in 1 2
do

cd ${logDir[$i]
echo $i
#Find file with .log extension, of type file, and modified prior last 14 days. Check if file is open and if it is clear contents.
find ./ -name "*.log" -type f -mtime +7 | while read file ; do lsof $file >/dev/null && >$file; done

#Find file with .log extension, of type file, and modified prior last 14 days. Check if file is open and if it isn't remove it.
find ./ -name "*.log" -type f -mtime +7 | while read file ; do lsof $file >/dev/null || rm $file; done

#find ./ -name "*.log" -type f -mtime +7 | while read file ; do lsof $file >/dev/null && >$file || rm $file; done
echo$i
done

Open in new window

0
Comment
Question by:Lico_w
  • 4
  • 3
7 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 250 total points
ID: 34196609
Hi again,

do you mean lines 25 and 28 work as you posted them above, but line 30 doesn't?

If so, this is somehow unbelievable!

I just tested it on one of my AIXs to and fro, and it would always run, commandline or script, even with bash instead of ksh.

Even if the $file variable should be empty for some reason the shell would complain about "||" not being expected, but never about ">"!

No idea, indeed!

You could try && cat /dev/null >"$file" instead of && >$file but I fear this won't help much.

wmp



0
 

Author Comment

by:Lico_w
ID: 34197050
Hi again, my apologies there are 5 commnet lines missing from the top so it is actually line 25 I'm getting the error on. I commented everything else out to enable me to try to test it.

Starngely if I strip that line ot and put it in another script it works, just need to work out what is making it have a wobbly now.
0
 

Assisted Solution

by:Lico_w
Lico_w earned 0 total points
ID: 34197250
Ok cracked it simple typo after the cd command on line 22 - missing '}'. Thanks for your help anyway
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34197258
OK, found it!

That's a very, very nice error! And such a meaningful error message!

The culprit is the missing closing curly bracket } here (line  22 above):

cd ${logDir[$i]
which should be:
cd ${logDir[$i]}

Seems that ksh tries to compose a variable name starting from the opening bracket till it comes to the > sign, which is obviously not allowed in variable names. Interesting that all the "#" and newline stuff should be allowed! I think I will have to study ksh a bit more!

Nice indeed!

wmp
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 34197274
1 minute late! Rats!
0
 

Author Comment

by:Lico_w
ID: 34197277
Thanks again for your help WMP
0
 

Author Closing Comment

by:Lico_w
ID: 34224597
.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now