![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Archive Script-Urgent | indira | HP-UX | 1 | 05-16-2007 02:31 PM |
| Archive Script-Urgent | indira | Shell Programming and Scripting | 0 | 05-16-2007 12:45 PM |
| Archive script old files | kayarsenal | Shell Programming and Scripting | 1 | 08-25-2006 02:46 AM |
| Urgent help required in deleting a line without opening a file usinga shell script | naan | Shell Programming and Scripting | 6 | 07-20-2006 04:42 AM |
| script to archive all the log files | tintedwindow | Shell Programming and Scripting | 0 | 06-13-2006 11:51 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Script to archive log files:Urgent Help required
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 #----------------------------------------------------------------------------- |
|
||||
|
You need a space before the opening bracket in function get_file_yearmm{
Is that it? You said a lot of errors. If there are more, please post all of them (after fixing what you can; you seem to have the same error with get_last_n_yearmm). |
|
||||
|
Thanx era i fixed that error.
when use function syntax like this : ---------------------------------- function get_file_yearmm{ #code } ----------------------------------- Errors am getting related to the variables: variable_name:unexpected but using the syntax i over come those: get_file_yearmm() { #code } -------------- Now up to function calls, code is working fine.I debug some errors.Now am getting error about $file like this : log_cln.sh[140]: $file: is not an identifier Am attatching my modified code. Thanks for replying. |
|
||||
|
The error message indicates line 140, but it seems to correspond with line 142 (and also on line 159) in your script: the syntax of the for statement is wrong. You should remove the dollar sign in front of the variable name; the correct syntax is Code:
for file in $LOGDIR Please remove the "urgent help" tag from this thread; tags are meant to help users of this site. |
![]() |
| Bookmarks |
| Tags |
| archive log files, date arithmetic |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|