Help with case and exit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with case and exit
# 1  
Old 05-02-2010
Help with case and exit

Hi all,

Am writing a script that uses the case statement and it is not working the way that I expect it to be. Script so far is as below.

What am expecting to happen is if the user enter neither Y or YES or N or NO, it is supposed to exit out of the script running only on_exit, otherwise, it should run everything under the MAIN section. What's happening at the moment is it is running everything under main regardless of whether it is Y or YES, N or NO or otherwise.

Not sure what am doing wrong or whether this is the expected behaviour of the script based on how I've written it so far.

Any response will be very much appreciated. Thanks in advance.

I've just started, script so far is as below:

Quote:
#!/usr/bin/ksh

#############
# VARIABLES #
#############
this_process=$$
this_script=`basename $0 | cut -f1 -d.`
this_report="${this_script}.report"
cat /dev/null > ${this_report}

#count_args=$#

#############
# VARIABLES #
#############
this_process=$$
this_script=`basename $0 | cut -f1 -d.`
this_report="${this_script}.report"
cat /dev/null > ${this_report}

##################
# FUNCTION / SUB #
##################

##################
# SUB : on_start #
##################
on_start()
{
echo ""
echo "Press CTRL-C to ABORT or <ENTER> to CONTINUE ... \c"
read dummy
echo ""

echo ""
echo "+---------------------------------------------------------------------+"
echo ""
echo "Start running <- ${this_script} ->"
echo "DATE : `date`"
echo ""
echo ""
}

#################
# SUB : on_exit #
#################
on_exit()
{
echo ""
echo "+---------------------------------------------------------------------+"
echo "Search and remove tmp files on `pwd` ..."
echo " `pwd`"
echo "+---------------------------------------------------------------------+"
echo ""

for tmpfile in `ls -1 ${this_script}.tmp.*`
do
echo "rm $tmpfile"
rm $tmpfile
done

echo ""
echo "Check the report file -> ${this_report}"
echo ""

exit 0
}

########################
# SUB : check_argument #
########################
check_argument()
{
echo "+----------------------------------------------+"
echo " Database running on `hostname` :"
echo " DATE : `date`"
echo "+----------------------------------------------+"

echo ""
echo "Or you set on the current database to check [Y/N] ??? \c"
read v_database_set
echo ""
v_database_set=`echo ${v_database_set} | tr [:lower:] [:upper:]`

case ${v_database_set} in
Y|YES) v_db=$ORACLE_SID
echo ""
ps -ef | grep _pmon | grep -v grep
echo ""
;;
N|NO) echo ""
ps -ef | grep _pmon | grep -v grep
echo ""

echo ""
echo "Enter the database SID to check : \c"
read v_db
echo ""

ps -e -o args | grep ora_pmon | grep -v grep > ${this_script}.tmp.00
cat /dev/null > ${this_script}.tmp.01
while read line
do
db=`echo $line | cut -c10-${$}`
echo ${db} >> ${this_script}.tmp.01
done < ${this_script}.tmp.00

if [ -z "`grep -i "^${v_db}$" ${this_script}.tmp.01`" ] ; then
echo ""
echo "You provided an invalid SID ... exiting ..."
echo ""
on_exit
exit 9
else
v_db=`grep -i "^${v_db}$" ${this_script}.tmp.01`
fi
;;
*) echo "Invalid answer !!! Exiting ..."
echo ""
on_exit
;;
esac


echo "DEBUG : v_db -> ${v_db}"
}

#######################
# SUB : report_01 #
#######################
report_01()
{
echo
echo "Running report_01 ..."
echo

sleep 5
}

#######################
# SUB : report_02 #
#######################
report_02()
{
echo
echo "Running report_02 ..."
echo

sleep 5
}

########
# MAIN #
########

on_start | tee -a ${this_report}
check_argument | tee -a ${this_report}
report_01 | tee -a ${this_report}
report_02 | tee -a ${this_report}

on_exit | tee -a ${this_report}

###########
# THE END #
###########
# 2  
Old 05-03-2010
Hi
There is no exit status handling after the call to the function check_argument, and hence report_01 and report_02 is also getting called. Handle the exit status of check_argument and also send some specific status in the check_argument function.

Thanks
Guru.

Last edited by pludi; 05-03-2010 at 01:34 PM..
# 3  
Old 05-03-2010
Quote:
Originally Posted by guruprasadpr
Hi
There is no exit status handling after the call to the function check_argument, and hence report_01 and report_02 is also getting called. Handle the exit status of check_argument and also send some specific status in the check_argument function.

Thanks
Guru.

Hi Guru,

Thanks for your response.

From your suggestion, that means I've made the wrong assumption that the exit command will exit out of the script.

BTW, is my checking under case correct though, i.e. the Y|YES thingy?

I put some echo line for testing and am expecting the echo of ${v_database_set} should show whatever I enter when prompted by the read v_database_set line. How come that does not seem to be the case? That is, the value of v_database_set is null/empty? Am expecting it should show Y, N or whatever I enter when prompted by the read command. It seemed like the variable is local to check_argument, is that how it is supposed to be?

Output from running the script:

Code:
+---------------------------------------------------------------------+

Start running <- x ->
DATE : Mon May  3 16:25:52 EST 2010


+----------------------------------------------+
 Database running on vinhns-nwp01 :
 DATE : Mon May  3 16:25:52 EST 2010
+----------------------------------------------+

Or you set on the current database to check [Y/N] ??? x

v_database_set --> X ...
Invalid answer !!! Exiting ...


+---------------------------------------------------------------------+
Search and remove tmp files on /oracle/BIP/102_64/OZONE/scripts/sql ...
         /oracle/BIP/102_64/OZONE/scripts/sql
+---------------------------------------------------------------------+

ls: x.tmp.*: No such file or directory

Check the report file -> x.report

v_database_set -->  ...

Running report_01 ...


Running report_02 ...


+---------------------------------------------------------------------+
Search and remove tmp files on /oracle/BIP/102_64/OZONE/scripts/sql ...
         /oracle/BIP/102_64/OZONE/scripts/sql
+---------------------------------------------------------------------+

ls: x.tmp.*: No such file or directory

Check the report file -> x.report



Echo placed on the script:


Code:
########################
# SUB : check_argument #
########################
check_argument()
{
   echo "+----------------------------------------------+"
   echo " Database running on `hostname` :"
   echo " DATE : `date`"
   echo "+----------------------------------------------+"

   echo ""
   echo "Or you set on the current database to check [Y/N] ??? \c"
   read v_database_set
   echo ""
   v_database_set=`echo ${v_database_set} | tr [:lower:] [:upper:]`

  echo "v_database_set --> ${v_database_set} ..."
   case ${v_database_set} in
      Y|YES) v_db=$ORACLE_SID
             echo ""



......
......
......

########
# MAIN #
########

on_start                | tee -a ${this_report}
check_argument  | tee -a ${this_report}

echo "v_database_set --> ${v_database_set} ..."
sleep 5

report_01               | tee -a ${this_report}
report_02               | tee -a ${this_report}

on_exit                 | tee -a ${this_report}


Last edited by pludi; 05-03-2010 at 01:35 PM..
# 4  
Old 05-03-2010
Hi
The checking of Y|YES is perfectly fine. Whatever variable you have it is local to the function. In case, if you want to have a return value, you need to use something like as shown below;

Code:
#!/usr/bin/ksh

fun() {
echo  25
}

x=`fun `
echo $x

As shown above, any echo you do inside the function can be collected as the return value from the calling function. The above echo will print 25. If you have more than one echo inside the function, your $x will be a string of all the echo statements outputs inside.

Thanks
Guru.

Last edited by pludi; 05-03-2010 at 01:38 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Change first letter of a word from lower case to upper case

Hi all, I am trying to find a way to change first letter in a word from lower case to upper case. It should be done for each first word in text or in paragraph, and also for each word after punctuation like . ; : ! ?I found the following command sed -i 's/\s*./\U&\E/g' $@ filenamebut... (7 Replies)
Discussion started by: georgi58
7 Replies

2. Shell Programming and Scripting

Conversion from Upper Case to Lower Case Condition based

Hello Unix Gurus : It would be really appreciative if can find a solution for this . I have records in a file . I need to Capitalize the records based on condition . For Example i tried the following Command COMMAND --> fgrep "2000YUYU" /export/home/oracle/TST/data.dat | tr '' ''... (12 Replies)
Discussion started by: tsbiju
12 Replies

3. Shell Programming and Scripting

[Solved] Change Upper case to Lower case in C shell

Is there a command that can switch a character variable from UPPER case to lower case? like foreach AC ( ABC BCD PLL QIO) set ac `COMMAND($AC)` ... end Thanks a lot! (3 Replies)
Discussion started by: rockytodd
3 Replies

4. Shell Programming and Scripting

sed ignoring case for search but respecting case for subtitute

Hi I want to make string substitution ignoring case for search but respecting case for subtitute. Ex changing all occurences of "original" in a file to "substitute": original becomes substitute Origninal becomes Substitute ORIGINAL becomes SUBSTITUTE I know this a little special but it's not... (1 Reply)
Discussion started by: kmchen
1 Replies

5. Shell Programming and Scripting

Not able to exit from case statements

Hi all, I wrote the following simple shell script to perform addition, subtraction, multiplication and division. In the below program, i am not able to exit from the script Shell Script ----------- #!/bin/sh bgcal() { cal="" echo "Enter the Option Number: \c" read cal if then... (3 Replies)
Discussion started by: uxpassion
3 Replies

6. Shell Programming and Scripting

exit from case - called in a function

Hello Experts, I am building a shell where I need to use case structure. The structure is in a function as in the sample code below: # Shell mySh #!/bin/sh doThis(){ var=$1 case "$var" in IT) echo "ok 1 $var" ;; ... (7 Replies)
Discussion started by: hkansal
7 Replies

7. Shell Programming and Scripting

Script needed to select and delete lower case and mixed case records

HELLO ALL, URGENTLY NEEDED A SCRIPT TO SELECT AND DELETE LOWER AND MIXED CASE RECORDS FROM A COLUMN IN A TABLE. FOR EXAMPLE : Table name is EMPLOYEE and the column name is CITY and the CITY column records will be: Newyork washington ... (1 Reply)
Discussion started by: abhilash mn
1 Replies

8. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

9. UNIX for Dummies Questions & Answers

lower case to upper case string conversion in shell script

How can convert a Lower case variable value to an upper case in the kron shell script. (3 Replies)
Discussion started by: dchalavadi
3 Replies
Login or Register to Ask a Question