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
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>
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.