shell script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script help
# 1  
Old 01-21-2008
shell script help

Hiii,
I have a requiremnt like this---

1) Utility will be run under MT owner.
2) This utility will clean files in ABC/envsw/logs. And following logs will be backed up or deleted.

Dispatcher Logs
Middle tier Logs
Sage log
Sage monitor log
Sage db clean up result log
Core files

3) mt_clean -a<action> [-t <time>]* -L <backup location>

-a<action> - has two values "delete" or "backup”. If not specified by default "delete" action will*occur. If*"backup" option is given, files will be backed up on the location specified by the -L, -L is mandatory if back up option is specified.

-t<time>* - The time is given with the -t option and its default is 1am
A time of 'now' means to execute the cleanup operation now
A time of 'never' means that the cleanup operation should not be scheduled and any existing cleanup should be cancelled.

-L <backup location> -*this field is mandatory if backup action is selected.

Note: As middle tier process are running and not stopped while this utility is running, the utility should create an empty*file with same name which has deleted or backed up. Otherwise middle tier logs will not be generated until the middle tier process is restarted. Any core files will be backed up or deleted according to the action chosen.*

It could be run as---

intasclean -a delete/backup now somelocation

I wrote the script like this....

Code:
RELEASE=${PATH##*/OCU_}
RELEASE=${RELEASE%%/bin:*}

DEBUGGING=''
WhenToRun="01:00"
ExpirationTime=7 # Number of days

FilesToBackup='*.track* *.xml *.vm* *.gz Trace* TRACE* *.core *.out fcif_data_* esi_error_* *.rollback *.sed R.* APStatus_*log* *.output* send_mail* downenv* check_env* intaspurge_db_* sqlnet.log *.rpt *.html *.csv *.log*'
FilesToDelete='*.log*'
FileLocation='$INTAS_INSTALL_DIR/envsw/logs'

PID_Templates='Trace\([0-9][0-9][0-9][0-9][0-9]\)_.* TRACE_\([0-9][0-9][0-9][0-9][0-9]\)_.* .*_\([0-9][0-9]*\).out'
Progname=$(basename $0)

Usage="Usage:$Progname"'[-a <delete or backup] [-t <time>] [-l|-L <backup location>]' 
     IsCronJob=0
if [[ "$1" = "cron" ]]; then
	. ~/.kshrc
	IsCronJob=1
	if [[ -z "$INTAS_INSTALL_DIR" ]]; then
		echo "You must be logged in as the application owner to delete the middle tier logs"
		return 1
	
fi

BackupLocation="$INTAS_INSTALL_DIR/mt_backup"
TEMPDIR=/tmp
if [[ -d $INTAS_INSTALL_DIR/envsw/tmp ]]; then
	TEMPDIR=$INTAS_INSTALL_DIR/envsw/tmp
fi

mt_clean_errfile="$INTAS_INSTALL_DIR/envsw/logs/mt_clean.$(date +%a)"

cfile=$(/bin/ls -1t $(find $INTAS_INSTALL_DIR -follow -name 'CommonScriptSetup.ksh') | head -1)

if [[ -x $cfile ]]; then
	export PATH=$PATH:$(dirname $cfile)
	. $cfile
else
	echo "Unable to find common include file needed for execution"
        if [[ $IsCronJob -eq 1 ]]; then
	  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
	fi
	return 1
fi
   
while getopts a:t:l:L:d optvar
do
	case $optvar in
	a) FilesToDelete="OPTARG"	   	
	   case $1 in
	     delete)
                           for files in $FileLocation
		 do
		touch $files $files.$(date +%a)
	#       find /intasmut2/envsw/logs -type f -mtime -2 | xargs grep "*.log*"
		rm -f /intasmut2/envsw/logs/$FilesToDelete
		echo -n "MiddleTier Logs are cleaned up"
		done
                ;;
             backup) 	        if [[ ! -d $BackupLocation ]]; then
		    echo "Non-Existed Directory Specified"
		else
		   cp /intasmut2/envsw/logs $BackupLocation
		fi
                ;;
                          esac
	     	
	t) WhenToRun="$OPTARG"
		if [[ $WhenToRun = "never" ]]; then
			WhenToRun='24:00'
		fi
		if [[ $WhenToRun = "now" ]]; then
			IsCronJob=1
			WhenToRun='24:00'
		fi
		if /bin/echo $WhenToRun | egrep '^[0-9]{1,2}:[0-5][0-9]$' >/dev/null; then
		  : Time is correct format
		else
			echo "Incorrect time format, time must be in HH:MM format"
			return $E_INT_INVALID_ARGS
		fi
		;;
	l) BackupLocation="$OPTARG"
		if [[ ! -d $BackupLocation ]]; then
			echo "Non-existant directory specified"
        		if [[ $IsCronJob -eq 1 ]]; then
	  		  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
			fi
			return $E_INT_MISSING_DIR
		fi
		if [[ $BackupLocation != *backup ]]; then
			echo "Appending backup subdirectories"
			BackupLocation=$BackupLocation/backup
			mkdir -p $BackupLocation >/dev/null 2>&1
  		if [[ ! -d $BackupLocation ]]; then
    		echo "Unable to make backup directory: $BackupLocation"
        		if [[ $IsCronJob -eq 1 ]]; then
	  		  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
			fi
			return $E_INT_MISSING_DIR
  		fi
		fi
		l_flag=Y
		Llcron=l
		;;
	L) BackupLocation="$OPTARG"
		Parent=$(expr $BackupLocation : '\(.*\)/.*' \| $BackupLocation)
		if [[ ! -d $Parent ]]; then
			echo "Non-existant directory specified"
        		if [[ $IsCronJob -eq 1 ]]; then
	  		  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
			fi
			return $E_INT_MISSING_DIR
		fi
		if [[ $IsCronJob -eq 1 ]]; then
			BackupLocation=$BackupLocation$(date +%w)
			if [[ ! -d $BackupLocation ]]; then
				mkdir -p $BackupLocation >/dev/null 2>&1
	  		if [[ ! -d $BackupLocation ]]; then
	    		echo "Unable to make backup directory: $BackupLocation"
			  if [[ $IsCronJob -eq 1 ]]; then
	  		    SendIntasNotifyMail "Middletier error message" $mt_clean_errfile
			  fi
			  return $E_INT_MISSING_DIR
	  		fi
			fi
			fi
		L_flag=Y
		Llcron=L
		;;
	d) DEBUGGING='/bin/echo'
		;;
        
	*) echo $Usgae
	   return 1
		;;
	esac
done
(( optcount = OPTIND - 1 ))
shift $optcount

if [[ -z "$1" ]]; then
  echo $Usage
  echo
  echo 'This command will schedule an INTAS cleanup at a certain time of the day'
  echo 'The time is given with the -t option and its default is 1am'
	echo "A time of 'now' means to execute the cleanup operation now"
  echo "A time of 'never' means that the cleanup operation should not be scheduled"
  echo "and any existing cleanup or the specified environments/centers should be cancelled"
  echo 'A backup location can be specified with the -l option. This is the directory'
  echo 'where files are moved that are older than 1 day.  The default is '"$INTAS_INSTALL_DIR/backup."
	echo 'Instead of -l, you can use -L to specify a 7-day backup directory rotation.'
  echo
  echo 'A file expiration interval is set with the -e option. Files older than this'
  echo 'number of days will be removed from the backup areas'
  echo
   echo "More than one cleanup can be scheduled as long as each cleanup is different"
  echo "e.g. "$Progname all" and "$Progname prd all" can both be run at the same or different times"
  echo
  echo "Typical usage: $Progname -t 2:00 -e 14 prd all"
  echo "This will schedule backups at 2am each night, purge files more than 14 days old"
  echo "and backup all centers in the prd environment"
  echo
	echo "Alternatively: $Progname -t now -e 14 prd all"
	echo "This will execute intasclean right now for one time"
  echo
  return $E_INT_INVALID_ARGS
fi

#Unzip the file under backup directory
/usr/contrib/bin/gunzip -r $BackupLocation

if [[ "$L_flag" = "Y" && "$l_flag" = "Y" ]]; then
	echo 'You can not specify both -l and -L options.'
	if [[ $IsCronJob -eq 1 ]]; then
	    SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
	fi
	return 1;
fi

if [[ ! -d $BackupLocation && -w $BackupLocation ]]; then
  echo 'The backup location does not exist or is not writable'
  if [[ $IsCronJob -eq 1 ]]; then
    SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
  fi
  return 1
fi

cd /
if whence $Progname >/dev/null; then
	: We found this program in the path
else
	echo "Unable to locate $Progname in the PATH, check the .kshrc for correctness"
        if [[ $IsCronJob -eq 1 ]]; then
    	  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
   	fi
	return $E_INT_NO_FILE
fi

arg1=$1
arg2=$2

cd $INTAS_INSTALL_DIR/envsw
if [[ $? != 0 ]]; then
	echo "Unable to cd to $INTAS_INSTALL_DIR/envsw, exiting..."
        if [[ $IsCronJob -eq 1 ]]; then
    	  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
   	fi
	return $E_INT_MISSING_DIR
fi

dirlist=""
for dir in $DirCleanupList
do
	if [[ -d $dir ]]; then
		dirlist="$dirlist $dir"
	fi
done
DirCleanupList=$dirlist

if [[ -z "$DirCleanupList" ]]; then
	echo "No directories to clean up"
        if [[ $IsCronJob -eq 1 ]]; then
    	  SendIntasNotifyMail "Middletierclean error message" $mt_clean_errfile
   	fi
	return 1
fi

echo "Start middletierclean at $(date)"

echo "Cleaning the following dirs:"
echo "$DirCleanupList" | perl -ne 'print join("\n", split(" ")) . "\n"; '

if [[ $IsCronJob -ne 1 ]]; then
  # We are not a cron job, install a line into the crontab
	if (crontab -l 2>&1 1>/dev/null | grep 'not authorized'); then
		echo "Unable to use cron, contact your system administrator"
		echo "Check that $(whoami) has an entry in cron.allow"
		return 1
	fi
	cronfile="$TEMPDIR/cur_crontab$$"
	crontab -l 2>&1 | grep -v '^crontab' >$cronfile
	if grep "$Progname cron .* $arg1 $arg2" $cronfile >/dev/null; then
		tmpvar=$(grep -v "$Progname cron.*$arg1 $arg2" $cronfile)
		echo "$tmpvar" >$cronfile
	fi
	if [[ $WhenToRun != 24:00 ]]; then
		Llcron=l
		minute=$(/bin/echo $WhenToRun | cut -d: -f2)
		hour=$(/bin/echo $WhenToRun | cut -d: -f1)
		if [[ -z "$DEBUGGING" ]]; then
			echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -e $FileLocation -$Llcron $BackupLocation $arg1 $arg2" >>$cronfile
		else
			echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Progname) cron -d -e $FileLocation -$Llcron $BackupLocation $arg1 $arg2" >>$cronfile
		fi
	fi
	crontab $cronfile >/dev/null 2>&1
	if crontab -l | diff - $cronfile >/dev/null; then
		if [[ $WhenToRun != 24:00 ]]; then
			echo crontab has been installed correctly
		else
			echo The crontab entry for \"$arg1 $arg2\" has been removed
		fi
	else
	  echo "Error installing crontab file"
	fi
	/bin/rm -f $cronfile
  
echo "End middletierclean at $(date)"
return $E_NO_ERROR

End of the shell script


Some of the portion of this script is preexisting.i have to wrote it like this only.I am not aware of getopts in scripts,i tried to google it also but nt able to understand,can you people please help me out in this.
getopts is required in this or anything that will be similar to this.I am getting errors .What i came to know is that the value of OPTARG is getting unset that is why the errors are coming.Give me some suggestions.I am in a learning phase so need help of the experts in this.

Last edited by namishtiwari; 01-22-2008 at 07:34 AM..
# 2  
Old 01-21-2008
Moderators and the experts in this forum help me out with this script.
I have HP-UX flavour of unix.
# 3  
Old 01-22-2008
MySQL

If any one has doubts please let me know,i will try to make u understand.I am also trying to debug the script.Experts help me out in this.
I Appericiate your help in advance.
# 4  
Old 01-22-2008
Bug

If any one has doubts please let me know,i will try to make u understand.I am also trying to debug the script.Experts help me out in this.
I Appericiate your help in advance
# 5  
Old 01-23-2008
1)
.....
a) FilesToDelete="OPTARG"
.....
Shouldn't it be $OPTARG instead of OPTARG ?

2 ) put a ;; after the "esac" at the end of the second "case"

3) you need a "fi" after this

if [[ -z "$INTAS_INSTALL_DIR" ]]; then
echo "You must be logged in as the application owner to delete the middle tier logs"
return 1

4) you need a "fi" for this if
# 6  
Old 01-23-2008
correction for 4)
you need a fi for this if:
if [[ $IsCronJob -ne 1 ]]; then
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script newbie- how to generate service log from shell script

Hi, I am totally a newbie to any programming languages and I just started an entry level job in an IT company. One of my recent tasks is to create a script that is able to show the log file of linux service (i.e. ntpd service) lets say, if I run my script ./test.sh, the output should be... (3 Replies)
Discussion started by: xiaogeji
3 Replies

2. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

3. Shell Programming and Scripting

How to write config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

4. UNIX for Dummies Questions & Answers

How to write Config shell script to pass variables in master shell script?

Dear Unix gurus, We have a config shell script file which has 30 variables which needs to be passed to master unix shell script that invokes oracle database sessions. So those 30 variables need to go through the database sessions (They are inputs) via a shell script. one of the variable name... (1 Reply)
Discussion started by: dba1981
1 Replies

5. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

6. Shell Programming and Scripting

Correct shell script to Call One shell script from another shell script

Hi All, I have new for shell scripting. Problem : I have one scrip at serv1 and path of server is /apps/dev/provimage/scripts and script name:extract_ancillary.bat. I need to call this script at server2(my working server) and execute at server2 . Please let me know how to build the... (5 Replies)
Discussion started by: Vineeta Nigam
5 Replies

7. Shell Programming and Scripting

call another shell script and pass parameters to that shell script

Hi, I basically have 2 shell scripts. One is a shell script will get the variable value from the user. The variable is nothing but the IP of the remote system. Another shell script is a script that does the job of connecting to the remote system using ssh. This uses a expect utility in turn. ... (2 Replies)
Discussion started by: sunrexstar
2 Replies

8. Shell Programming and Scripting

How to use ssh execute other shell script on other host (shell script include nohup)?

i want use ssh on the host01 to execute autoexec.sh on the host02 like following : host01> ssh host02 autoexec.sh autoexec.sh include nohup command like follwing : nohup /home/jack/deletedata.sh & after i execute ssh host02 autoexec.sh one the host01. i can't found deletedata.sh... (1 Reply)
Discussion started by: orablue
1 Replies

9. Shell Programming and Scripting

invoking a shell script inside cgi shell script

Hi, I have an HTML form through which I get some text as input. i need to run a shell script say script.sh inside a perl-cgi script named main_cgi.sh on the form input. I want to write the contents of the form in a file and then perform some command line operations like grep, cat on the text... (2 Replies)
Discussion started by: smriti_shridhar
2 Replies

10. Shell Programming and Scripting

How to Run a shell script from Perl script in Parent shell?

Hi Perl/UNIX experts, I have a problem in running a shell script from my perl script (auto.pl). I run the perl script using perl auto.pl from the shell prompt The shell script picks the files in "input" folder and procesess it. The shell script blue.sh has this code. export... (16 Replies)
Discussion started by: hifake
16 Replies
Login or Register to Ask a Question