Register
It is currently Thu Sep 18, 2014 1:40 am

Optimising the script for LOG file rotation


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Fri May 13, 2011 12:08 am   

Joined: Thu May 12, 2011 2:19 pm
Posts: 2
Location: Netherlands
Hi,

I would need some help in optimizing the BASH shell script. I am running RedHat Linux and need to run this script every month to rotate the log files. There should always be log files from *.1 ... *.12 in the folder, but never above that number (not sure, of the script takes care of it).

Can someone proofread my script and if there is something I could optimize, I would be VERY happy to LEARN from.

Many thanks!
D
__

Code:
#!/bin/bash

# Get SDE parameters
cd /etc/EPDA/SDE
. ./sde-settings-global
. ./sde-settings-local

## OUTSIDE LOOP - Go through all DDDDD instances

## Sourcing the DDDDD configuration file
cd $SDE_ENVDIR
SDE_SF_LIST=`ls arc*`
for SDE_SF in $SDE_SF_LIST ; do
  . ./$SDE_SF
LOG_PATH="$SDEHOME/etc"
cd $LOG_PATH

## Filling in the log filenames (3 different ones) for DDDDD
SDE_LOG=`ls *.log | grep -v "upgrade\|setup\|giomgr_" | grep "sde_" | sort`
GIO_LOG=`ls *.log | grep -v "upgrade\|setup\|sde_" | grep "giomgr_" | sort`
SDC_LOG=`ls *.log | grep -v "upgrade\|setup\|sde_" | grep -i "sdedc_" | sort`

### Creating a sequence 1-13 for the output backup files
for i in `seq 1 13` ; do
  SEQ="$i $SEQ"
done

### Rotate the file
for i in $SEQ ; do
  mv bkp-sde-log.$i bkp-sde-log.$(($i + 1))
  mv bkp-gio-log.$i bkp-gio-log.$(($i + 1))
  mv bkp-sdc-log.$i bkp-sdc-log.$(($i + 1))
done

### Remove the file named with the 13th extension
rm -f *.13

## Copy the content of the current three DDDDD log files to the sequenced ones
cat $SDE_LOG > bkp-sde-log.1
cat $GIO_LOG > bkp-gio-log.1
cat $SDC_LOG > bkp-sdc-log.1

## Clear the content of the DDDDD log files
echo > $SDE_LOG
echo > $GIO_LOG
echo > $SDC_LOG


## OUTSIDE LOOP - End
done


Top
 Profile  
 PostPosted: Fri May 13, 2011 3:54 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 546
Hi,

you should write a new version of your script without using cat, grep, ls, seq
BASH can do all these things.

for example :
SDE_SF_LIST should be an array not using `ls` but globs (take care to set nullglob)
don't use `seq`, BASH can {1..13}, and straightfully `for i in {1..13}; ...`
grep can be replaced by `if [[ $var == any?(ext)glob ]]; then ...`

then BASH becomes a lot more fun :)


Top
 Profile  
 PostPosted: Fri May 13, 2011 5:03 am   

Joined: Thu May 12, 2011 2:19 pm
Posts: 2
Location: Netherlands
Hi,

I never used globs, so how should the script be modified, a bit lot here.


Top
 Profile  
 PostPosted: Fri May 13, 2011 5:32 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
ssddgreg wrote:
Hi,

I never used globs, so how should the script be modified, a bit lot here.

For example, instead of
Code:
SDE_SF_LIST=`ls arc*`
for SDE_SF in $SDE_SF_LIST ; do

use
Code:
for SDE_SF in arc* ; do


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP