Solved

UNIX BASH Script to Clear Log Files

Posted on 2010-11-23
7
940 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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
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

Independent Software Vendors: 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

Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:
Suggested Courses

635 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