Solved

return a variable with string value in case satement

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Linux haproxy: stop temporary haproxy service 9 41
installing docker on ubuntu 6 28
Backup Raspberri Pi over the netowrk to a Windows Share 5 41
number in printf 13 27
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.

785 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