need urgent help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need urgent help
# 1  
Old 02-15-2008
need urgent help

Code:
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
	done

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
# 2  
Old 02-15-2008
Put a "set -x" command at the top of the script and run it. You should now be able to debug the executed code and find where is the problem.
# 3  
Old 02-15-2008
Quote:
Originally Posted by robotronic
Put a "set -x" command at the top of the script and run it. You should now be able to debug the executed code and find where is the problem.
yes that is also possible with this-
ksh -x "filename"

I will make atry,can you please have a look too and suggest me something if you find anything wrong.I will apperciate your help.

The actual output i am getting 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

it should not do it instead it should display the the Usage and come out if any location is not specified.
Thanks

Last edited by namishtiwari; 02-15-2008 at 08:46 AM..
# 4  
Old 02-18-2008
Here how i can set the value of isBackup,because of this only the problem is coming.I need help in seting the value of this variable.
# 5  
Old 02-18-2008
Quote:
Originally Posted by namishtiwari
Here how i can set the value of isBackup,because of this only the problem is coming.I need help in seting the value of this variable.
After the case...esac statements end and before the while loops ends, check for the following:
If action flag is set, and if location flag is not provided, then print usage.

I see that you handle the location flags -l and -L separately. I dont see any difference in them except for BackupLocation=$BackupLocation/backup$(date +%w) in the -L set.
You can do this instead
Code:
case $optvar in 
a)...
l|L)
  if [ flag is L ] ; then
    BackupLocation=$BackupLocation/backup$(date +%w)
  endif
t)...

# 6  
Old 02-18-2008
Hi vino,

i have changed the script a bit and now for
mt_clean -a mtBackup

it is showing me the desired output..

but for the delete option ie mtDelete,i need not to give any location,it will by defualt delete the files from the default location.

When i ran this

mt_clean -a mtDelete

it is showing me the same output as for the above ie--
Code:
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

your suggestions are required.
# 7  
Old 02-18-2008
Quote:
Originally Posted by namishtiwari
Hi vino,

i have changed the script a bit and now for
mt_clean -a mtBackup

it is showing me the desired output..

but for the delete option ie mtDelete,i need not to give any location,it will by defualt delete the files from the default location.

When i ran this

mt_clean -a mtDelete

it is showing me the same output as for the above ie--
Code:
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

your suggestions are required.
So what did you change ?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

URGENT Reading a file and assessing the syntax shell script URGENT

I am trying to write a shell script which takes an input file as an arguement in the terminal e.g. bash shellscriptname.sh input.txt. I would like for the file to be read line by line each time checking if the .txt file contains certain words or letters(validating the syntax). If the line being... (1 Reply)
Discussion started by: Gurdza32
1 Replies

2. Shell Programming and Scripting

Urgent !!

Hello All, I want to use scp for copying multiple files ( files locations are stored in an array ) from remote server from different locations without prompting password every time . I will supply password once and it should be able to copy every file mentioned in an array. eg :- array have ... (1 Reply)
Discussion started by: manpav
1 Replies

3. Shell Programming and Scripting

Need urgent help

Hi geeks, I'm trying to write a below script, but it throws an error, please check and correct me. #!/bin/bash #The below script will extract the string error1 error2 and error3 and also it will count and list the occurrence count1='grep -i "error1" test.txt | wc -l' count2='grep -i "error2"... (4 Replies)
Discussion started by: naren nandale
4 Replies

4. Shell Programming and Scripting

Need Urgent help

Hi friends, I need urgent help here: Issue: I need to create shell script that will find the files & throw an error through job (autosys) when file not found. Daily we use to receive 3 files from a system. Obstacles: 1) All 3 files names are same. 2) Timestamp is same. 3)... (1 Reply)
Discussion started by: tush
1 Replies

5. Shell Programming and Scripting

Urgent!!

Hi I have a file containing DE 3'UTR in Homo sapiens alpha-1-B glycoprotein (A1BG), mRNA. SQ Sequence 216 BP; 37 A; 58 C; 69 G; 52 T; 0 other; DE 3'UTR in Homo sapiens alpha-1-B glycoprotein (A1BG), mRNA. SQ Sequence 1844 BP; 358 A; 483 C; 434 G; 569 T; 0 other; DE 3'UTR in Homo... (1 Reply)
Discussion started by: jyotirmoy21
1 Replies

6. Red Hat

urgent

abb 117.96.113.21 cgg 101.2.104.42 cgg 110.227.247.236 desk 203.20.35.28 png 1.39.242.241 png 1.39.242.241 rzx 101.2.104.42 rzx 115.246.160.36 abb 49.138.242.187 how to find the count of this file wtrto ip thnx in advance (4 Replies)
Discussion started by: himanshu1.singh
4 Replies

7. UNIX for Advanced & Expert Users

need help urgent...........

Hi friends.. I am using the below command to search few files from many folders which is under one folder.. i mean let say the path is A/B/C...and inside C...i have 1-10 folder... the below command is working fine.... for i in 1 3 5 7; do find /A/B/C/${i} -name "*.txt" -o -name "*.csv"... (3 Replies)
Discussion started by: sapan123
3 Replies

8. UNIX for Advanced & Expert Users

URGENT,URGENT- Need help tape drive installation

Hi, I am trying to attach tape drive to sun V890 running Solaris 9 on it. I have installed HBA(qlogic) in slot 1 of 0-8 slots and booted the system. I do not see HBAin prtdiag output. The tape drive is not attached to HBA. The tape drive I am going to attach is Sony AIT3. 1.How can I make... (3 Replies)
Discussion started by: sriny
3 Replies

9. Solaris

Need Help Urgent

Hi. My E250 server (Solaris 8) running oracle database gets hangup in between. I checked the logs in /var/adm/messages but could not find anything related to this. can anyone help me out? bala (3 Replies)
Discussion started by: balaji_prk
3 Replies
Login or Register to Ask a Question