Solved

return a variable with string value in case satement

Posted on 2013-05-30
4
464 Views
Last Modified: 2013-06-24
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
Comment
Question by:sk0227
4 Comments
 
LVL 34

Assisted Solution

by:johnsone
johnsone earned 250 total points
ID: 39208692
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
 
LVL 73

Accepted Solution

by:
sdstuber earned 250 total points
ID: 39208728
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
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39208828
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now