Urgent hep is required

Reply

Join Date: Aug 2007
Posts: 49
Reputation: larryperl is an unknown quantity at this point 
Solved Threads: 0
larryperl larryperl is offline Offline
Light Poster

Urgent hep is required

 
0
  #1
Feb 15th, 2008
Shell Scripting Syntax (Toggle Plain Text)
  1. Programname=$(basename $0) # While scheduling to cron, program name is needed.
  2.  
  3. Usage="Usage: $Programname [-a mtDelete/mtBackup] [-l/-L Backuplocation] [-t now/cron/hh:mm]"
  4.  
  5. IsCronJob=1 # default, script will run at now.
  6. isBackUp=0 # default, it will take back up.
  7. lFlag=0 # it is optional. default value is zero
  8. LFlag=0 # it is optional. dfault value is zero
  9. ERR=1
  10.  
  11. LOGDIR="$INTAS_INSTALL_DIR/envsw/logs"
  12. BackupLocation="$INTAS_INSTALL_DIR/mt_backup"
  13.  
  14. mtlogFile="/tmp/mtclean.log.$$"
  15.  
  16. if [[ $# -lt 1 ]];
  17. then
  18. echo "$Usage"
  19. return $ERR
  20. fi
  21.  
  22. if [[ ! -d $BackupLocation ]];
  23. then
  24. mkdir -p $BackupLocation 2>/dev/null
  25. fi
  26.  
  27. while getopts a:l:L:t: optvar
  28. do
  29. case $optvar in
  30. a) action="$OPTARG"
  31. if [[ $action = "mtBackup" ]];
  32. then
  33. isBackUp=1
  34. fi
  35. ;;
  36. l) BackupLocation="$OPTARG"
  37. if [[ $lFlag -eq 1 ]]; then
  38. echo "Either option l or L should be given to $Programname"
  39. echo "$Usage"
  40. echo "$Programname is terminated"
  41. return $ERR;
  42. fi
  43. if [[ $isBackUp -eq 1 ]];
  44. then
  45. givenPath="$BackupLocation"
  46. if [[ ! -d $BackupLocation ]];
  47. then
  48. echo "Non-existant directory specified"
  49.  
  50. mkdir -p $BackupLocation >/dev/null 2>&1
  51. if [[ $? = 0 ]];
  52. then
  53. echo "$Prograname creates the dirctory $BackupLocation"
  54. else
  55. echo "$Programname could not able to create the directory $BackupLocation"
  56. return $ERR
  57. fi
  58. else
  59. echo "Backup directory already exists"
  60. fi
  61. lFlag=1
  62. Llcron=l
  63. else
  64. echo "$Programname is executed with -l option without action mtBackup"
  65. echo "$Usage"
  66. echo "$Programname is terminated"
  67. return $ERR
  68. fi
  69. ;;
  70. L) BackupLocation="$OPTARG"
  71. if [[ $LFlag -eq 1 ]]; then
  72. echo "Either option l or L should be given to $Programname"
  73. echo "$Usage"
  74. echo "$Programname is terminated"
  75. return $ERR;
  76. fi
  77. if [[ $isBackUp -eq 1 ]];
  78. then
  79. givenPath="$BackupLocation"
  80. BackupLocation=$BackupLocation/backup$(date +%w)
  81. if [[ ! -d $BackupLocation ]];
  82. then
  83. echo "Non-existant directory $BackupLocation"
  84. mkdir -p $BackupLocation >/dev/null 2>&1
  85. if [[ $? = 0 ]];
  86. then
  87. echo "$Prograname creates the dirctory $BackupLocation"
  88. else
  89. echo "$Programname could not able to create the directory $BackupLocation"
  90. return $ERR
  91. fi
  92. else
  93. echo "Backup directory $BackupLocation is already exists"
  94. fi
  95. lFlag=1
  96. Llcron=L
  97. else
  98. echo "$Programname is executed with -L option without action mtBackup"
  99. echo "$Programname is terminating"
  100. return $ERR;
  101. fi
  102. ;;
  103. t) WhentoRun="$OPTARG"
  104. #echo "$WhentoRun"
  105. if [[ $WhentoRun = "cron" ]];
  106. then
  107. IsCronJob=1
  108. echo "$Programname is running from cron"
  109. elif [[ $WhentoRun = "now" ]];
  110. then
  111. IsCronJob=1
  112. echo "$Programname starts Now"
  113. else
  114. if /bin/echo $WhentoRun | egrep '^[0-9]{1,2}:[0-5][0-9]$' >/dev/null; then
  115. echo "$Programname is scheduled to $WhentoRun at cron"
  116. hour=$(/bin/echo $WhentoRun | cut -d: -f1)
  117. minute=$(/bin/echo $WhentoRun | cut -d: -f2)
  118. IsCronJob=0
  119. else
  120. echo "Incorrect time format, time must be in HH:MM format"
  121. return $ERR
  122. fi
  123.  
  124. fi
  125. ;;
  126. *) echo $Usage
  127. return $ERR
  128. ;;
  129. esac
  130. done
  131.  
  132. if [[ ! -w $BackupLocation ]];
  133. then
  134. echo "$BackupLocation is not writable"
  135. return $ERR;
  136. fi
  137.  
  138. if whence $Programname >/dev/null; then
  139. : We found this program in the path
  140. else
  141. echo "Unable to locate $Progname in the PATH, check the .kshrc for correctness"
  142. return $ERR
  143. fi
  144.  
  145. cd $LOGDIR
  146. if [[ $? != 0 ]];
  147. then
  148. echo "Unable to cd to $INTAS_INSTALL_DIR/envsw, exiting..."
  149. return $ERR
  150. fi
  151.  
  152. if [[ ! -d $LOGDIR && -w $LOGDIR ]];
  153. then
  154. echo "$LOGDIR directory does not exist or not writable."
  155. echo "Please check the user permission"
  156. return $ERR
  157. fi
  158.  
  159.  
  160. # If mtclean is exceuted from cron or with option now, we can skip below scheduling part.
  161. if [[ $IsCronJob -eq 0 ]];
  162. then
  163. if (crontab -l 2>&1 1>/dev/null | grep 'not authorized');
  164. then
  165. echo "Unable to use cron, contact your system administrator"
  166. echo "Check that $(whoami) has an entry in cron.allow"
  167. return $ERR;
  168. else
  169. cronfile="crontab"
  170. cronfile="$INTAS_INSTALL_DIR/envs/tmp/cur_crontab$$"
  171. crontab -l 2>&1 | grep -v '^crontab' >$cronfile
  172. if grep "$Programname .* cron .*" $cronfile >/dev/null; then
  173. tmpvar=$(grep -v "$Progname .* cron .*" $cronfile)
  174. echo "$tmpvar" >$cronfile
  175. fi
  176.  
  177. #echo "Cron: $cronfile"
  178. echo "$minute $hour" '* * *' ". ~/.kshrc; $(whence $Programname) -a $action -$Llcron $givenPath -t cron >>$mtlogFile" >> $cronfile
  179. fi
  180. crontab $cronfile >/dev/null 2>&1
  181. if crontab -l | diff - $cronfile >/dev/null; then
  182. if [[ $WhenToRun != 24:00 ]]; then
  183. echo crontab has been installed correctly
  184. else
  185. echo "Error in crontab entry"
  186. fi
  187. else
  188. echo "Error in installing crontab"
  189. fi
  190. else
  191. curmonth=$(date +%b)
  192. curdate=$(echo "$(date +%c)" | cut -d' ' -f3)
  193.  
  194. if [[ $curdate = "" || $curdate = " " ]];
  195. then
  196. curdate=$(echo "$(date +%c)" | cut -d' ' -f4)
  197. today="$curmonth $curdate"
  198. else
  199. today="$curmonth $curdate"
  200.  
  201. fi
  202.  
  203. FileList=$(find . -type f -name "*") # List all the files.
  204.  
  205. echo "Starting moving/removing files from $LOGDIR"
  206. extrafiles1=$(ls mt.*.*.*.* 2>/dev/null)
  207. extrafiles2=$(ls *core* 2>/dev/null)
  208. for file in $FileList
  209. do
  210. if [[ $(ls -l $file 2>/dev/null) = *$today* ]];
  211. then
  212. echo "Skips $file file. It is today's file"
  213. cp $file $BackupLocation 2>/dev/null
  214. cat /dev/null >$file
  215. continue
  216.  
  217. fi
  218.  
  219. if [[ $isBackUp -eq 1 ]];
  220. then
  221. mv $file $BackupLocation 2>/dev/null
  222.  
  223. else
  224. rm $file $BackupLocation 2>/dev/null
  225.  
  226. 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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 520
Reputation: techniner is an unknown quantity at this point 
Solved Threads: 19
techniner techniner is offline Offline
Posting Pro

Re: Urgent hep is required

 
0
  #2
Feb 15th, 2008
What is the actual out put it is giving?
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: larryperl is an unknown quantity at this point 
Solved Threads: 0
larryperl larryperl is offline Offline
Light Poster

Re: Urgent hep is required

 
0
  #3
Feb 15th, 2008
Originally Posted by techniner View Post
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
Last edited by larryperl; Feb 15th, 2008 at 7:45 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Urgent hep is required

 
0
  #4
Feb 16th, 2008
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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: larryperl is an unknown quantity at this point 
Solved Threads: 0
larryperl larryperl is offline Offline
Light Poster

Re: Urgent hep is required

 
0
  #5
Feb 18th, 2008
Originally Posted by eggi View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Urgent hep is required

 
0
  #6
Feb 18th, 2008
Hey there,

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

For this line:

Shell Scripting Syntax (Toggle Plain Text)
  1. while getopts a:l:L:t: optvar

add a line on top, like this:

Shell Scripting Syntax (Toggle Plain Text)
  1. Lflag=1
  2. while getopts a:l:L:t: optvar

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

Shell Scripting Syntax (Toggle Plain Text)
  1. if [[ $lFlag -eq 1 ]]; then
  2. echo "Either option l or L should be given to $Programname"
  3. echo "$Usage"
  4. echo "$Programname is terminated"
  5. return $ERR;
  6. fi

to this:

Shell Scripting Syntax (Toggle Plain Text)
  1. lFlag=0

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

Shell Scripting Syntax (Toggle Plain Text)
  1. *) echo $Usage
  2. return $ERR
  3. ;;
  4. esac
  5. done

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

Shell Scripting Syntax (Toggle Plain Text)
  1. *) echo $Usage
  2. return $ERR
  3. ;;
  4. esac
  5. done
  6. if [[ $lFlag -eq 1 ]]; then
  7. echo "Either option l or L should be given to $Programname"
  8. echo "$Usage"
  9. echo "$Programname is terminated"
  10. return $ERR;
  11. 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
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: larryperl is an unknown quantity at this point 
Solved Threads: 0
larryperl larryperl is offline Offline
Light Poster

Re: Urgent hep is required

 
0
  #7
Feb 18th, 2008
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--
Shell Scripting Syntax (Toggle Plain Text)
  1. lFlag=1
  2. LFlag=1
  3. while getopts a:l:L:t: optvar
-----------------------------------------------
Shell Scripting Syntax (Toggle Plain Text)
  1. l) BackupLocation="$OPTARG"
  2. lFlag=0
-------------------------------------------------
Shell Scripting Syntax (Toggle Plain Text)
  1. L) BackupLocation="$OPTARG"
  2. LFlag=0

-----------------------------------------------
Shell Scripting Syntax (Toggle Plain Text)
  1. *) echo $Usage
  2. return $ERR
  3. ;;
  4. esac
  5. done
  6. if [[ $lFlag -eq 1 || $LFlag -eq 1 ]]; then
  7. echo "Either option l or L should be given to $Programname"
  8. echo "$Usage"
  9. echo "$Programname is terminated"
  10. return $ERR;
  11. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Urgent hep is required

 
0
  #8
Feb 18th, 2008
Hey there,

Is it still not working?

Thanks

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Urgent hep is required

 
0
  #9
Feb 18th, 2008
Ah, just noticed

This new line you added:

Shell Scripting Syntax (Toggle Plain Text)
  1. 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

Shell Scripting Syntax (Toggle Plain Text)
  1. if [[ $lFlag -eq 1 && $LFlag -eq 1 ]]; then

or

Shell Scripting Syntax (Toggle Plain Text)
  1. if [[ $lFlag -eq 1 -a $LFlag -eq 1 ]]; then

Cheers

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: larryperl is an unknown quantity at this point 
Solved Threads: 0
larryperl larryperl is offline Offline
Light Poster

Re: Urgent hep is required

 
0
  #10
Feb 18th, 2008
Originally Posted by eggi View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum


Views: 2474 | Replies: 16
Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC