Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 482
  • Last Modified:

return a variable with string value in case satement

Hello Experts,

i am writing a shell script to find the Prime Time and Non Prime time. based on the the values
i need to send alert to dba team and oncall dba team.

here is my consideration :

during the prime time Monday to Friday 8:00 AM to 6:00 PM, any alerts raised by database should page/email all the members in dba group
runing non prime time Sunday to Sunday 6:00 PM to 8:00 AM, any alert raised by database should go to on-call dba.

i wrote small function to determine prime time and not prime time.


here is the function:

sp1db1 oracle $ cat check_prime.sh
#!/bin/sh
_primeTime()
{
PTIME="YES"
NPTIME="YES"
host_TZ=`echo $TZ`
TZ=US/Central; export TZ
PTSTART=8
PTSTOP=18
DAY=`date +%a`
HOUR=`date +%H`
TZ=$host_TZ; export TZ
if [ "$HOUR" -ge "$PTSTART" ] && [ "$HOUR" -lt "$PTSTOP" ]; then
        case $DAY in Mon|Tue|Wed|Thu|Fri) return $PTIME; esac
fi
return $NPTIME
}
_primeTime

i am getting error when i executed this shell script;

sp1db1 oracle $ /bin/sh -x check_prime.sh
+ _primeTime
PTIME=YES
NPTIME=YES
+ echo US/Mountain
host_TZ=US/Mountain
TZ=US/Central
+ export TZ
PTSTART=8
PTSTOP=18
+ date +%a
DAY=Thu
+ date +%H
HOUR=12
TZ=US/Mountain
+ export TZ
+ [ 12 -ge 8 ]
+ [ 12 -lt 18 ]
+ return YES
check_prime.sh: YES: bad number


could you please guide me to fix this error
0
sk0227
Asked:
sk0227
2 Solutions
 
johnsoneSenior Oracle DBACommented:
Why do you need to return a string?  Also, I don't see how your function can return anything other than YES.

Why not do something like this:

#!/bin/sh

PTIME=1
NPTIME=2
_primeTime()
{
host_TZ=`echo $TZ`
TZ=US/Central; export TZ
PTSTART=8
PTSTOP=18
DAY=`date +%a`
HOUR=`date +%H`
TZ=$host_TZ; export TZ
if [ "$HOUR" -ge "$PTSTART" ] && [ "$HOUR" -lt "$PTSTOP" ]; then
        case $DAY in Mon|Tue|Wed|Thu|Fri) return $PTIME; esac
fi
return $NPTIME
}

_primeTime
isPrimeTime=$?
if [ $isPrimeTime -eq 1 ] ; then
    # Commands for prime time
else
    # Commands for not prime time
fi

Open in new window

0
 
sdstuberCommented:
return wants a numeric parameter, not a string,  use echo

#!/bin/sh
_primeTime()
{
PTIME="YES"
NPTIME="NO"
host_TZ=`echo $TZ`
TZ=US/Central; export TZ
PTSTART=8
PTSTOP=18
DAY=`date +%a`
HOUR=`date +%H`
TZ=$host_TZ; export TZ
if [ $HOUR -ge $PTSTART ] && [ $HOUR -lt $PTSTOP ]; then
    case $DAY in Mon|Tue|Wed|Thu|Fri) echo $PTIME;;
         *) echo $NPTIME;;
    esac
else
   echo $NPTIME
fi
}
_primeTime

Open in new window



or, if you want to use return to cause an early exit from the function,  then echo first


#!/bin/sh
_primeTime()
{
PTIME="YES"
NPTIME="NO"
host_TZ=`echo $TZ`
TZ=US/Central; export TZ
PTSTART=8
PTSTOP=18
DAY=`date +%a`
HOUR=`date +%H`
TZ=$host_TZ; export TZ
if [ $HOUR -ge $PTSTART ] && [ $HOUR -lt $PTSTOP ]; then
    case $DAY in Mon|Tue|Wed|Thu|Fri) echo $PTIME; return;; esac
fi
echo $NPTIME
}
_primeTime

Open in new window

0
 
MikeOM_DBACommented:
You can do all (monitor db and send e-mail alerts) using Enterprise Manager (dbconsole or Grid Control or Cloud Control) which allow setting up notification schedules for each administrator.
:p
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now