Solved

UNIX BASH Script to Clear Log Files

Posted on 2010-11-23
7
934 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

860 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