Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UNIX BASH Script to Clear Log Files

Posted on 2010-11-23
7
Medium Priority
?
952 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 1000 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
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!

 
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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

916 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