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:

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:

+---------------------------------------------------------------------+

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:

########################
# 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}

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;

#!/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.