Solved

UNIX BASH Script to Clear Log Files

Posted on 2010-11-23
7
936 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

710 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