Solved

return a variable with string value in case satement

Posted on 2013-05-30
4
469 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 74

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

821 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