Sponsored Content
Full Discussion: Help with case and exit
Top Forums Shell Programming and Scripting Help with case and exit Post 302418066 by newbie_01 on Sunday 2nd of May 2010 10:32:40 PM
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 #
###########
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
All times are GMT -4. The time now is 06:45 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy