Programname=$(basename $0)  # While scheduling to cron, program name is needed.

Usage="Usage: $Programname [-a mtDelete/mtBackup] [-l/-L Backuplocation] [-t now/cron/hh:mm]"

IsCronJob=1     # default, script will run at now.
isBackUp=0	# default, it will take back up.
lFlag=0		# it is optional. default value is zero
LFlag=0		# it is optional. dfault value is zero
ERR=1
 
LOGDIR="$INTAS_INSTALL_DIR/envsw/logs"
BackupLocation="$INTAS_INSTALL_DIR/mt_backup"

mtlogFile="/tmp/mtclean.log.$$"

if [[ $# -lt 1 ]];
then
	echo "$Usage"
	return $ERR
fi

if [[ ! -d $BackupLocation ]];
then
	mkdir -p $BackupLocation 2>/dev/null
fi

while  getopts a:l:L:t: optvar
do
	case $optvar in
	a) action="$OPTARG"
		if [[ $action = "mtBackup" ]];
		then
			isBackUp=1
		fi
		;;
	l) BackupLocation="$OPTARG"
		if [[ $lFlag -eq 1 ]]; then
			echo "Either option l or L should be given to $Programname"
			echo "$Usage"
			echo "$Programname is terminated"
			return $ERR;
		fi
		if [[ $isBackUp -eq 1 ]];
		then	
			givenPath="$BackupLocation"
			if [[ ! -d $BackupLocation ]];
			then
				echo "Non-existant directory specified"
				
				mkdir -p $BackupLocation >/dev/null 2>&1
	                        if [[ $? = 0 ]];
        	                then
					echo "$Prograname creates the dirctory $BackupLocation"
				else
					echo "$Programname could not able to create the directory $BackupLocation"
					return $ERR
				fi
			else
				echo "Backup directory already exists"
			fi
			lFlag=1
			Llcron=l
		else
			echo "$Programname is executed with -l option without action mtBackup"
			echo "$Usage"
			echo "$Programname is terminated"
			return $ERR
		fi
		;;
	L) BackupLocation="$OPTARG"
		if [[ $LFlag -eq 1 ]]; then
			echo "Either option l or L should be given to $Programname"
			echo "$Usage"
			echo "$Programname is terminated"
			return $ERR;
		fi
		if [[ $isBackUp -eq 1 ]];
		then
		        givenPath="$BackupLocation"
			BackupLocation=$BackupLocation/backup$(date +%w)
			if [[ ! -d $BackupLocation ]];
			then
				echo "Non-existant directory $BackupLocation"
				mkdir -p $BackupLocation >/dev/null 2>&1
	                        if [[ $? = 0 ]];
        	                then
					echo "$Prograname creates the dirctory $BackupLocation"
				else
					echo "$Programname could not able to create the directory $BackupLocation"
					return $ERR
				fi
			else
				echo "Backup directory $BackupLocation is already exists"
			fi
			lFlag=1
			Llcron=L
		else
			echo "$Programname is executed with -L option without action mtBackup"
			echo "$Programname is terminating"
			return $ERR;
		fi
		;;
	t) WhentoRun="$OPTARG"
		#echo "$WhentoRun"
		if [[ $WhentoRun = "cron" ]];
		then
			IsCronJob=1
			echo "$Programname is running from cron"
		elif [[ $WhentoRun = "now" ]];
		then
			IsCronJob=1
			echo "$Programname starts Now"
		else
			if /bin/echo $WhentoRun | egrep '^[0-9]{1,2}:[0-5][0-9]$' >/dev/null; then
	                	echo "$Programname is scheduled to $WhentoRun at cron"
				hour=$(/bin/echo $WhentoRun | cut -d: -f1)
				minute=$(/bin/echo $WhentoRun | cut -d: -f2)
				IsCronJob=0
			else
                        	echo "Incorrect time format, time must be in HH:MM format"
                        	return $ERR
                	fi
		
		fi
		;;
	*) echo $Usage
	   return $ERR 
	  	;;
	esac
done

if [[ ! -w $BackupLocation ]];
then
	echo "$BackupLocation is not writable"
	return $ERR;
fi

if whence $Programname >/dev/null; then
        : We found this program in the path
else
        echo "Unable to locate $Progname in the PATH, check the .kshrc for correctness"
	return $ERR
fi

cd $LOGDIR
if [[ $? != 0 ]]; 
then
        echo "Unable to cd to $INTAS_INSTALL_DIR/envsw, exiting..."
	return $ERR
fi

if [[ ! -d $LOGDIR && -w $LOGDIR ]];
then
	echo "$LOGDIR directory does not exist or not writable."
	echo "Please check the user permission"
	return $ERR
fi


# If mtclean is exceuted from cron or with option now, we can skip below scheduling part.
if [[ $IsCronJob -eq 0 ]]; 
then
        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 $ERR;
 	else
		cronfile="crontab"	
		cronfile="$INTAS_INSTALL_DIR/envs/tmp/cur_crontab$$"
	        crontab -l 2>&1 | grep -v '^crontab' >$cronfile
        	if grep "$Programname .* cron .*" $cronfile >/dev/null; then
                	tmpvar=$(grep -v "$Progname .* cron .*" $cronfile)
                	echo "$tmpvar" >$cronfile
        	fi

		#echo "Cron: $cronfile"
		echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Programname) -a $action -$Llcron $givenPath -t cron >>$mtlogFile" >> $cronfile
        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 "Error in crontab entry"
		fi
	else
		echo "Error in installing crontab"
	fi
else
	curmonth=$(date +%b)
	curdate=$(echo "$(date +%c)" | cut -d' ' -f3)

	if [[ $curdate = "" || $curdate = " " ]];
	then
		curdate=$(echo "$(date +%c)" | cut -d' ' -f4)
		today="$curmonth  $curdate"
	else
		today="$curmonth $curdate"
			
	fi

	FileList=$(find . -type f -name "*")	# List all the files.
	
	echo "Starting moving/removing files from $LOGDIR"
        extrafiles1=$(ls mt.*.*.*.* 2>/dev/null) 
	extrafiles2=$(ls *core* 2>/dev/null)
	for file in $FileList
	do
		if [[ $(ls -l $file 2>/dev/null) = *$today* ]]; 
		then
			echo "Skips $file file. It is today's file"
			cp $file $BackupLocation 2>/dev/null
			cat /dev/null >$file
			continue
					        
		fi
	        
		if [[ $isBackUp -eq 1 ]];
		then
			mv $file $BackupLocation 2>/dev/null
			
		else
			rm $file $BackupLocation 2>/dev/null
                        
		fi

The usage is mentuioned above of the script.
when i am running the script it is not throwing any error but not giving me the desired output for the one case that is--

mt_clean -a mtbackup
or
mt_clean -a mtBackup

first of all it is taking both without displaying any error and taking the doing some weird thing which i do not want.
first of all it should display a message that mtbackup is specified without -l/-L option.

can anybody help me out in this.

Thanks

What is the actual out put it is giving?

it is giving me the desired outputs but when i put only

mt_clean -a mtbackup

it is copying the files at the default location but i want it like this

whne i put
mt_clean -a mtbackup

then it should not do anything instead of doing copy it should come out displaying the Usage.
the actual output is--
mt_clean -a mtBackup
Starting moving/removing files from /intasmut2/envsw/logs
Skips ./sage_db_clean_up.result file. It is today's file
Skips ./mt.log.8882.-5 file. It is today's file
Skips ./sage.log.5 file. It is today's file
Skips ./mt.log.8882.-6 file. It is today's file
Skips ./Trace28482main1 file. It is today's file
Skips ./dispatcher.log.8881.-6 file. It is today's file
Skips ./Trace28591main1 file. It is today's file
mt_clean is completed suffessfully


Need help in this.
Thanks

Hey There,

You're checking whether or not the -l OPT is being passed in the option argument, so if the -l flag isn't passed to the program, it won't know to even check if there's an error.

I would recommend setting the lFlag variable to whatever the "off" value is, before parsing your opt args, and only set it to "on" if the option is called. Then, after processing your opt args, do the check for the value of $lFlag.

That should take care of it for you.

Hope that helps :)

, Mike

Hey There,

You're checking whether or not the -l OPT is being passed in the option argument, so if the -l flag isn't passed to the program, it won't know to even check if there's an error.

I would recommend setting the lFlag variable to whatever the "off" value is, before parsing your opt args, and only set it to "on" if the option is called. Then, after processing your opt args, do the check for the value of $lFlag.

That should take care of it for you.

Hope that helps :)

, Mike

Hi Mike,
can you explain me just giving me the sample change i need to put in my script.I aperciate your help.

Hey there,

I'll try to keep this brief, since the getopts statement is so huge ;)

For this line:

while  getopts a:l:L:t: optvar

add a line on top, like this:

Lflag=1
while  getopts a:l:L:t: optvar

Then, for the l) and L) options, change this part:

if [[ $lFlag -eq 1 ]]; then
		echo "Either option l or L should be given to $Programname"
		echo "$Usage"
		echo "$Programname is terminated"
                                return $ERR;
fi

to this:

lFlag=0

Then, after this part (the end of your getopts):

*) echo $Usage
	   return $ERR 
	  	;;
	esac
done

add your sanity check (updated to look like this):

*) echo $Usage
	   return $ERR 
	  	;;
	esac
done
if [[ $lFlag -eq 1 ]]; then
		echo "Either option l or L should be given to $Programname"
		echo "$Usage"
		echo "$Programname is terminated"
                                return $ERR;
fi

This way the lFlag variable will be verified after running getopts.

You can do this a number of ways. Hopefully, this was helpful enough ;)

Best Wishes,

, Mike

Hi Mike,

it is running fine for the option--

mt_clean -a mtBackup

but when i am running it for common usage of the script like--

mt_clean -a mtBackup -l /intasmut2/kp -t now

the program is terminated also.

I tell u the changes i did in the script--

lFlag=1
LFlag=1
while  getopts a:l:L:t: optvar

-----------------------------------------------

l) BackupLocation="$OPTARG"
		lFlag=0

-------------------------------------------------

L) BackupLocation="$OPTARG"
		LFlag=0

-----------------------------------------------

*) echo $Usage
	   return $ERR 
	  	;;
	esac
done
                        if [[ $lFlag -eq 1 || $LFlag -eq 1 ]]; then
			        echo "Either option l or L should be given to $Programname"
			        echo "$Usage"
			        echo "$Programname is terminated"
			        return $ERR;
		        fi

This is the primary use of the script

mt_clean -a mtBackup -l /intasmut2/abc -t now/time
or
mt_clean -a mtBackup -L /intasmut2/abc -t now/time

but it should not do it for --

mt_clean -a mtBackup

Ah, just noticed

This new line you added:

if [[ $lFlag -eq 1 || $LFlag -eq 1 ]]; then

is going to throw an error unless someone selects both the -l AND -L option.

Try changing that to

if [[ $lFlag -eq 1 && $LFlag -eq 1 ]]; then

or

if [[ $lFlag -eq 1 -a $LFlag -eq 1 ]]; then

Cheers :)

, Mike

Hey there,

Is it still not working?

Thanks :)

, Mike

It is working for the option which i mentioned in the script-
mt_clean -a mtBackup

but for

mt_clean -a mtbackup -lor -L /intasmut2/abc -t now/time

it is not working

Hey there,

Even with the mod to the "if" line I posted afterward?

Thanks

Mike

Still i am getting the same error--

>mt_clean -a mtBackup -l /intasmut2/kp -t now
>Backup directory already exists
>mt_clean starts Now
>Either option l or L should be given to mt_clean
>Usage: mt_clean [-a mtDelete/mtBackup] [-l/-L Backuplocation] [-t now/cron/hh:mm]
>mt_clean is terminated

but it should not do like this.
it should take the backup at the desired location.

I got the problem resolved for the issue.Actually in the script i was setting the flag to 1 at one more place so i removed that part.

Now the problem is coming for the delete part----

for deletion we ned not to specify any location,it will delete all the files at the default location.

when i ran the scaript as-

webmut2@france>mt_clean -a mtDelete
Either option l or L should be given to mt_clean
Usage: mt_clean [-a mtDelete/mtBackup] [-l/-L Backuplocation] [-t now/cron/hh:mm]
mt_clean is terminated

instaed of this it should delete the files at the default location,Help me in this please.

Thanks...

I am able to resolve the problem,thanks guys for yourvalueable inputs,SPecial thanks to Mike who gave his time to me.

Thanks.

Hey again,

Just in case there's a misunderstanding. When you say:

It is working for the option which i mentioned in the script-
mt_clean -a mtBackup

Does that mean that you get the error about the -l or -L options aren't being used?

and for:

mt_clean -a mtbackup -lor -L /intasmut2/abc -t now/time

it is not working

Do you mean that it's still throwing an error even though you've passed your program the option?

Just curious. If you can answer that I don't see the issue, I'll run it the next time I get a terminal

Thanks :)

, Mike

Hi,

With little modifications,i was able to run the script as i wanted.Tha options which u mentioned is working perfectlly fine.Your help is a great support.Hope to discuss some issues again with you in near future.If u want the whole script i can attach it for you without any problem.

Thanks.

Hey There,

No problem at all. If you want/need any extra help, you can PM me with entire script (I think - not sure about size requirements on the site) and I'd be happy to look at it.

Best wishes :)

, Mike

This article has been dead for over six months. Start a new discussion instead.