Day_of_the_week


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Day_of_the_week
# 1  
Old 06-07-2011
Day_of_the_week

Hi All,

HP-UX B.11.11 U 9000/800 3251073457

I am newbie to shell scripting. I have a code, which I need to write in a manner, where, if dates falls within specified time, it will exit else move ahead.
now, I have following code, which I want not to run during 17:00 pm saturday to 02:45 am sunday. I am not sure about the code, which I am trying to code. would really appriciate, if someone could guide me in that regard

Code:
#!/sbin/sh
SID=smrtprod
. qa02
set -x
Day_Of_Week=`date +%a`
Hour=`date +%H`
if [ $Day_Of_Week = "Sat" ]
then
if [ $Hour -ge 17:00 -a $Hour -le 02:45 ]
then
echo "In Maintenance Window, exiting out"
exit
else
echo "select 'DB up' from dual;" | sqlplus -s oramonitoring/temp123@smrtprod
if [ $? -eq 0 ]; then
:
else
echo " Database $SID has issue." | mailx -s "Database $SID is down, please act on this" CSC-ORACLE-DBA@Estee.com, 919642990137@nma.vodafone.in
fi
fi
fi
echo "successfull complete"
exit 0

hare krishna
# 2  
Old 06-07-2011
I think it is impossible to:
Code:
if [ $Hour -ge 17:00 -a $Hour -le 02:45 ]

hour can be be after 17:00 AND before 02:45

I think you want something closer to:
Code:
if [ ( $Day -eq "Sat" -a $Hour -gt 17:00 ) -o ( $Day -eq "Sun" -a $Hour -le 02:45 ) ]

although, need to think about the time a bit; not sure about those comparisons.
# 3  
Old 06-08-2011
Thanks for your thoughts. I have ran following code, but it's throwing an error.

Code:
#!/sbin/sh
SID=smrtprod
. qa02
if [ $Day -eq "Sat" -a $Hour -gt 17:00 -o $Day -eq "Sun" -a $Hour -le 02:45 ]
then
echo "In Maintenance Window, exiting out"
exit
else
echo "select 'DB up' from dual;" | sqlplus -s oramonitoring/temp123@smrtprod
if [ $? -eq 0 ]; then
:
else
echo " Database $SID has issue." | mailx -s "Database $SID is down, please act on this" CSC-ORACLE-DBA@Estee.com, 919642990137@nma.vodafone.in
fi
fi
echo "successfull complete"
exit 0

none> ./time.sh
./time.sh[4]: Sat: A test command parameter is not valid.

hare krishna

---------- Post updated 06-08-11 at 07:19 AM ---------- Previous update was 06-07-11 at 03:22 PM ----------

Anyone have any idea, why I am hiting this? Do I need to declare $Day variable, before I can use it. I thougt $Day might be the inbulit function.

hare krishna
# 4  
Old 06-08-2011
The "-eq" is for integer comparisons.

For string comparisons, use "=".

For more details on how to compare strings/integer, type "man test" on the unix prompt.

The comparison "$Hour -gt 17:00" will not work. First you must declare "Hour" as an integer and then remove ":" before assigning to it.

As if "Day" is a defined variable, type "set" in the unix prompt. If it does not appear in the list, it is not defined.

---------- Post updated at 12:41 PM ---------- Previous update was at 12:27 PM ----------

Here is one possible solution based on your requirement:
Code:
#!/usr/bin/ksh
typeset -i mHHMM=`date +%H%M`
mDay=`date +%a`
if [[ "${mDay}" = "Sat" && ${mHHMM} -gt 1700 ]]; then
  echo "Exiting because it is Saturday after 17:00"
  exit
fi
if [[ "${mDay}" = "Sun" && ${mHHMM} -lt 245 ]]; then
  echo "Exiting because it is Sunday before 2:45"
  exit
fi
<write your code here>

# 5  
Old 06-08-2011
Thank you very much shell. Also, will this following code will work ?

Code:
#!/sbin/sh
SID=smrtprod
. qa02
set -x
Day=`date +%a`
Hour=`date +%H`
if [ "$Day" = "Sat" ] then
if [ "$Day" -eq "Sat" -a $Hour -gt 17:00 -o "$Day" -eq "Sun" -a $Hour -le 02:45 ] ;
then
echo "In Maintenance Window, exiting out"
exit
else
echo "select 'DB up' from dual;" | sqlplus -s oramonitoring/temp123@smrtprod
if [ $? -eq 0 ] ; then
:
else
echo " Database $SID has issue." | mailx -s "Database $SID is down, please act on this"
fi
fi
echo "successfull complete"
exit 0

hare krishna
# 6  
Old 06-08-2011
2 questions :

when is your script supposed to run ?

can't you just manage it using a scheduler or a crontab ?
# 7  
Old 06-08-2011
Ans:-

1. It suppose to run in every 10 min, except for the maintainence window.
2. We can shedule it control-m, but we need to put the code inside the script.
 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question