I have no prior knowledge of Unix shell scripting,but my requriment demands to wrie a script that do fallowing things.
1.Delete older than one year log files
2.Moves files in to the directories as YYYYMM wise.
3.If files in $LOGDIR older than n=2 months tar and move them to $ARCHIVEDIR
4.Removing n=4 months older files from Archive directory
I write a script in ksh .But am getting lot of errors.Can any one help me out.My code is as fallows:
code:log_cln.sh
#!/bin/ksh
#*******************************************************************************
#** Program : log_clean.sh
#**
#** Job Name :
#**
#** Original Author :
#**
#** Description : Script used to archive and clean up files
#**
#** Revision History: Please do not stray from the example provided.
#**
#**
#*****************************************************************************
#test hook
. ~/.setup_env
. $FPATH/common_funcs.sh
#Define varibles used
L_SCRIPTNAME=`basename $0`
curr_year=`date +%Y`
curr_month=`date +%m`
curr_yearmm=`date +%Y%m`
#file_year
#file_mm
#file_yearmm variables to hold the file year,mm,yearmm values****
#last_n_yearmm
#
while getopts "s:t:i:d:f" option
do
case $option in
s) start_step=$OPTARG;;
t) data_tablespace=$OPTARG;;
i) index_tablespace=$OPTARG;;
d) debug=1;;
f) date_string=$OPTARG;;
esac
done
shift $(($OPTIND - 1))
#-----------------------------------------------------------------
# Set the default values for all options. This will only set the
# variables which were NOT previously set in the getopts section.
#-----------------------------------------------------------------
debug=${debug:=0}
#-----------------------------------------------------------------
#Check for debug mode [-d]
#-----------------------------------------------------------------
if [ $debug -eq 1 ]; then
set -x
fi
#-----------------------------------------------------------------
# Set $ parameters here.
#-----------------------------------------------------------------
#-------------------------------------------------------------------
#function which takes file's year, month name and retrieve yearmonth
value
#----------------------------------------------------------------------
function get_file_yearmm{ # inputs : file year and month name
#output :files yyyymm
year=$1
mon_name=$2
case $year in
?????)file_year=year;; #for year like 2007 ,wc is 5
*)file_year=curr_year ;; #for current year itz showing something like 11:45 ,wc 6.
esac
case "$mon_name" in
"Jan")file_mm=01;;
"Feb")file_mm=02;;
"Mar")file_mm=03;;
"Apr")file_mm=04;;
"May")file_mm=05;;
"Jun")file_mm=06;;
"Jul")file_mm=07;;
"Aug")file_mm=08;;
"Sep")file_mm=09;;
"Oct")file_mm=10;;
"Nov")file_mm=11;;
"Dec")file_mm=12;;
) echo " Invalid month" ;;
esac
file_yearmm=${file_year}$file_mm
}
#-------------------------------------------------------------------------------
#function which gives the last nth month yyyymm value from current date
#-------------------------------------------------------------------------------
function get_last_n_yearmm{
yyyy=curr_year
mm=curr_month
n=$1
while [ n -gt 0 ]
do
if [ mm -eq 01 ];then
yyyy=`expr $yyyy - 1`
mm=12
last_n_yearmm=${yyyy}$mm
else
mm=`printf %02d $(expr $mm - 1)`
last_n_yearmm=${yyyy}$mm
fi
n=`expr $n - 1`
done
}
#----------------------------------------------------------------
Function to check the return status and set the appropriate
# message
#-----------------------------------------------------------------
function check_status
{
if [ $? -ne 0 ]; then
err_msg="$L_SCRIPTNAME Errored at Step: $step_number"
echo "$err_msg"
subject_msg="Job Error - $L_SCRIPTNAME"
send_mail "$err_msg" "$subject_msg" "$ERR_LIST"
exit $step_number
fi
}
#-----------------------------------------------------------------
#Begin Main Program
#-----------------------------------------------------------------
check_variables LOGDIR ARCHIVEDIR
#-----------------------------------------------------------------
step_number=1
#Description: Delete older than one year log files
#
#-----------------------------------------------------------------
#
if [ $start_step -le $step_number ] ; then
echo "** Step Number $step_number.................."
echo "****Deletiing the files older than 1 year....."
cd $LOGDIR
for $file in $LOGDIR
do
file_year=$(ls -l $file | awk '{ print $8 }' )
file_year=$(get_file_yearmm $file_year)
if [ file_year -le $(expr $curr_year -1 ];then
rm -rf $file
check_status
fi
done
fi
#------------------------------------------------------------------
#-----------------------------------------------------------------
step_number=2
#Description: Moves files in to the directories as YYYYMM wise
#-----------------------------------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
echo " Moves files in to the directories as YYYYMM wise"
for $file in $LOGDIR
do
file_year=$(ls -l $file | awk '{ print $8 }')
file_mm=$(ls -l $file | awk '{ print $6 }')
file_yearmm=$(get_file_yearmm $file_year $file_mm)
mv $file ./${file_yearmm}/$file
check_status
done
fi
#---------------------------------------------------------------------
#-----------------------------------------------------------------
step_number=3
#Description: If files in $LOGDIR older than n=2 months tar and move
them to $ARCHIVEDIR
#-----------------------------------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
echo " tar and moving files to archive directory"
n=2
last_n_yearmm=$(get_last_n_yearmm $n)
cd $LOGDIR
for $file in $LOGDIR
do
file_yearmm=$(ls -l $file |grep "^d"| awk '{ print $9 }')
if [ file_yearmm -le last_n_yearmm ];then
tar -cvf `find . "*." -type d `>${file_yearmm}.tar 2>/dev/null
check_status
fi
done
mv $LOGDIR/".tar" ${ARCHIVEDIR}
check_status
fi
#------------------------------------------------------------------------
step_number=4
#Description: untar all the files
#
#-------------------------------------------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
cd $ARCHIVEDIR
for $file in $ARCHIVEDIR
do
tar -xvf ${file_yearmm}.tar
check_status
done
fi
#--------------------------------------------------------------------------
step_number=5
#Description: Removing n=4 months older files from Archive directory
#--------------------------------------------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
echo "Removing n=4 months older files from Archive directory"
n=4
last_n_yearmm=$(get_last_n_yearmm $n)
for $file in $ARCHIVEDIR
do
file_yearmm=$(ls -l $file | awk '{ print $9 }')
if [ file_yearmm -le last_n_yearmm ];then
rm -rf $file
check_status
fi
done
fi
#---------------------------------------------------------------------------
step_number=6
#---------------------------------------
#Description:6 tar all
#---------------------------------------
if [ $start_step -le $step_number ] ; then
echo "*** Step Number $step_number"
cd $ARCHIVEDIR
for $file in $ARCHIVEDIR
do
file_yearmm=$(ls -l $file |grep "^d"| awk '{ print $9 }')
tar -cvf $\{file_yearmm\} $\{file_year\}.tar
check_status
done
fi
exit 0
#-----------------------------------------------------------------------------