Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

return a variable with string value in case satement

Posted on 2013-05-30
4
Medium Priority
?
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 35

Assisted Solution

by:johnsone
johnsone earned 1000 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 1000 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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 the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

715 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