KSH: Confused with behaviour of exit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting KSH: Confused with behaviour of exit
# 1  
Old 12-09-2010
KSH: Confused with behaviour of exit

Hi Everyone,
I am confused on why the below snippet of code is not working as I intend it to do. I have googled and confirmed that "exit" is supposed to abort the execution of the script regardless if the exit was called from inside a function, or the main body of the script.
Code:
log_and_die() {
        # Print the error message and terminate the execution of the script
        typeset __MESSAGE_TYPE__="FATAL"
        typeset __MESSAGES__="$1"
        print "$__MESSAGES__" | while read line
        do
                write_log "$__MESSAGE_TYPE__" "$line"
        done
        exit 1
}
...
get_db_password() {
        #-- requires the database user as an argument
        if [ $# -ne 1 ]; then
                log_and_die "The function \"get_db_password()\" requires the username as an argument."
        fi
        typeset L_DB_USER="$1"
        typeset L_DB_PASS=""
        if [ ! -e $APP_CONFIG_DIR/$L_DB_USER.pub ]; then
                log_and_die "The public key \"$L_DB_USER.pub\" was not found in the config directory."
        fi
        if [ ! -e $APP_CONFIG_DIR/$L_DB_USER.key ]; then
                log_and_die "The private key \"$L_DB_USER.key\" was not found in the config directory."
        fi
        L_DB_PASS=$(decrypt -a aes -i $APP_CONFIG_DIR/$L_DB_USER.key -k $APP_CONFIG_DIR/$L_DB_USER.pub)
        if [ $? -ne 0 ]; then
                log_and_die "There was an error in decrypting the public / private key pair for $L_DB_USER."
        fi
 
        print "$L_DB_PASS"
}
...
run_query(){
        #-- Will take the following arguments
        #       1. username
        #       2. password
        #       3. database instance
        #       4. sql query to execute
        #       5. output destination
 if [ $# -ne 5 ]; then
                log_and_die "The function \"run_query()\" requires the following arguments: (1) username, (2) password, \
(3) db instance, (4) sql query file, (5) output file."
        fi
        typeset L_DB_USER="$1"
        typeset L_DB_PASS="$2"
        typeset L_DB_INSTANCE="$3"
        typeset L_SQL_QUERY="$4"
        typeset L_OUTPUT_FILE="$5"
        typeset L_OUTPUT
        #-- Perform validation on the arguments
        #-- Check if the database instance is live
        L_OUTPUT=`tnsping $L_DB_INSTANCE 2>&1`
        if [ $? -ne 0 ]; then
                print "$L_OUTPUT" | while read line
                do
                        log_warn "$line"
                done
                log_and_die "Unable to ping the database instance \"$L_DB_INSTANCE\"."
        fi
        #-- Check if the SQL query exists.
        if [ ! -e $L_SQL_QUERY ]; then
                log_and_die "Unable to find the sql file \"$L_SQL_QUERY\"."
        fi
L_OUTPUT=`sqlplus -s $L_DB_USER/$L_DB_PASS@$L_DB_INSTANCE <<EOF 2>&1
WHENEVER OSERROR EXIT FAILURE
WHENEVER SQLERROR EXIT FAILURE
SPOOL $L_OUTPUT_FILE
@$L_SQL_QUERY
EXIT
EOF`
        if [ $? -ne 0 ]; then
                print "$L_OUTPUT" | while read line
                do
                        log_warn "$line"
                done
                log_and_die "Errors were encountered while trying to execute the query \"$L_SQL_QUERY\"."
        fi
 
}

Using the below statement does not abort the script. Please take note that the file "ITM2.pub" does not exists so I am expecting the whole script to abort once it goes thru the function get_db_password().

Code:
run_query "ITM" "$(get_db_password ITM2)" "AUWMRCD2A" "$SQL_QUERY" "$REPORT"

-- or --
Code:
DB_PASS="$(get_db_password ITM2)"
...
run_query "ITM" "$DB_PASS" "AUWMRCD2A" "$SQL_QUERY" "$REPORT"

However, I tried the below statement and it worked fine meaning that the function get_db_password() and log_and_die() is properly working.
Code:
get_db_password ITM2

My question is, if you use a function as a parameter or assign the output of the function to a variable, will it make "exit" behave differently? If the answer is yes, is there any workaround available?
# 2  
Old 12-09-2010
By using $(get_db_password ITM2), the function is run inside a sub-shell. The "exit 1" exits this sub-shell and then it is back to the parent shell, where you can check the sub-shell's return code, which will be 1 and undertake appropriate action..

For example:
Code:
if ! DB_PASS="$(get_db_password ITM2)" ; then
  exit 1
fi


Last edited by Scrutinizer; 12-09-2010 at 03:44 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-09-2010
or the simpler:
Code:
DB_PASS="$(get_db_password ITM2)" || exit 1

This User Gave Thanks to jlliagre For This Post:
# 4  
Old 12-09-2010
Thanks for your replies guys. Actually I arrived at the same solution as what you have given. I guess I am making this too complicated that what it is supposed to be (not a native shell programmer).

Code:
DB_USER="ITM"
DB_PASS="$(get_user_pass $DB_USER)" || log_and_die "Failed to get the password for the user \"$DB_USER\"."

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Behaviour of pwd command in sh and ksh

I have a script as below. bash-3.00$ cat test.sh #!/usr/bin/ksh path=`pwd` echo $path var=$path/temp11 echo $var If run it is giving output bash-3.00$ ksh test.sh //var/tmp/SB2/miscellaneous //var/tmp/SB2/miscellaneous/temp11 (5 Replies)
Discussion started by: millan
5 Replies

2. Shell Programming and Scripting

Exit status of the ksh Script

Hi Im trying to write a script that will archive some file using java program.Below is the part of the script that I use and my problem is that the script always return with status 0.Below is part of my script(end part) purge.ksh echo "No of files before tar :... (4 Replies)
Discussion started by: saachinsiva
4 Replies

3. Shell Programming and Scripting

different behaviour for ksh and ksh -x

I'm getting different behaviour when executing below script in debug option. $ cat ss.ksh ff=$(pwd) echo " ff : $ff" $ ksh ss.ksh ff : /tmp $ ksh -x ss.ksh + + pwd ff= + echo ff : ff : I was getting this behaviour in my actuall script i'm able to reproduce this in simple script... (4 Replies)
Discussion started by: luckybalaji
4 Replies

4. Shell Programming and Scripting

Terminal is closing on exit in ksh

hi while executing the following script, my terminal window is getting closed if I enter a invalid option. I want the script should go back the the command prompt. how to do achive it. i execute the script as . ./test #! /usr/bin/ksh Printf " Type of Installer : \n\t\t 1. Whole Build... (3 Replies)
Discussion started by: vij_krr
3 Replies

5. Shell Programming and Scripting

KSH: Test telnet and exit

Hi, I need to do a test Telnet in KSH and if the connection is good then disconnect the telnet session with out logging in and without exiting the shell script. Example output of a good connection: $telnet xxx.xx.xx.xxx xxxx Trying xxx.xx.xx.xxx... Connected to xxx.xx.xx.xxx. Escape... (1 Reply)
Discussion started by: calex
1 Replies

6. Shell Programming and Scripting

HELP WITH .ksh script converting the exit status

Hi Can someone help me please? In a standard UNIX .ksh script, if you have the exit status..say 5...what line do you have to enter into the script for this number to be automatically converted to its actual exit reason by looking up the exit status file...wherever that is? thanks angus (1 Reply)
Discussion started by: angusyoung
1 Replies

7. UNIX for Advanced & Expert Users

Strange KSH behaviour - any comments?

As you are probably aware, $# indicates the number of parameters passed into a korn shell script. But this appears to hang around for sunsequent runs...???? A simple script:- #!/usr/bin/ksh echo "#parameters $#" echo "\$1 $1" echo "\$2 $2" I run the script with 0 parameters (all fine) #... (7 Replies)
Discussion started by: gsw_aix
7 Replies

8. UNIX for Dummies Questions & Answers

Longer commands and strange behaviour on ksh

Hi, I was trying to customize this archaic HP-UX box. only shell available is ksh and that too seems to be pretty old and doesn't completely conform to what I read on the web about ksh. Anyway here are my issues: - I wanted to have a dynamic title on xterm or dtterm. I put the following lines... (2 Replies)
Discussion started by: anurags
2 Replies

9. UNIX for Advanced & Expert Users

How to exit the KSH functions

Hi I am having the script which contains more functions. I want to exit the function if any failure. I tried with exit - the session itself is getting logged out. How can i fix this issue? (11 Replies)
Discussion started by: sharif
11 Replies

10. Programming

Exit Code in HP-UX KSH.

In one of my programs another process is called using the system command e.g. lv_error = system("myproc"); where lv_error is declared as an int. myproc would be returning 0 for success and 1 for failure. e.g. if (success) { return(0); }else{ return(1); } When the return code... (3 Replies)
Discussion started by: mbb
3 Replies
Login or Register to Ask a Question