exit from case - called in a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exit from case - called in a function
# 1  
Old 04-28-2009
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:
Code:
# Shell mySh
#!/bin/sh
 
doThis(){
 
        var=$1
 
        case "$var" in
        [CS]IT[1-4])
                echo "ok 1 $var"
                ;;
        PSDEV)
                echo "ok 2 $var"
                ;;
        PSSIT)
                echo "ok 2 $var"
                ;;      
        *)
                echo "not ok"
                exit
                ;;
        esac
}
 
echo `doThis "abcd"`
 
echo "here also i come, even after exiting "

output:
Code:
$ sh mySh
not ok
here also i come, even after exiting

I am trying to exit in the last condition, the default. But it is not happening here. Please tell me if I am making a mistake somewhere.
Do I need to check the exit status to make it exit from the script as a whole and not just from the function?

Thank you.

Regards,
HKansal
# 2  
Old 04-28-2009
Try giving exit with a number
exit 1
# 3  
Old 04-28-2009
Change

Code:
echo `doThis "abcd"`

to

Code:
doThis "abcd"

# 4  
Old 04-28-2009
It doesn't exit becuase calling the function spawns a sub process which then exits when you hit the not ok. It doesn't exit the parent shell script.

A couple of ways it can be done are either replacing exit with kill $$ (which will kill the parent pid and thus the shell script itself.

Or return a status from the function.

Code:

doThis(){
 
        var=$1
 
        case "$var" in
        [CS]IT[1-4])
                echo "ok 1 $var"
                return 0
                ;;
        PSDEV)
                echo "ok 2 $var"
                return 0
                ;;
        PSSIT)
                echo "ok 2 $var"
                return 0
                ;;      
        *)
                echo "not ok"
                return 1
                ;;
        esac
}

doThis "abcd" || { echo "Problem exiting script" ; exit ; }

echo "here also i come, even after exiting "

# 5  
Old 04-28-2009
Hello,

Thank you for helping.

@devtakh:
the status code with exit does not help Smilie. I tried that earlier too.

@lavascript n dennis
Although I ve not tried what u've told but thanks for answering the last part. The "exit" brings me out of the function, a subprocess as u said.
I'll just try this and get back. Actually I cannot do:
Code:
doThis "abcd"

as I need the value returned into a variable. I ll use return for that and a status code as told by lavascript. Lemme just try it and I'll let u know where I reached.

Thank You

Regards,
Hkansal
# 6  
Old 04-28-2009
If you assign the function to a variable you will be launching a sub process

e.g

function_output=$(doThis "abcd")

Then within the function you can exit by using the kill $$ method which will exit the script.

Or

do a check for $? which requires the return bits in the function

e.g

Code:
function_output=$(doThis "abcd")
if [ $? -ne 0 ];then
    echo "Last command i.e function failed"
    exit 
fi

echo "I shouldn't get here if function fails"

I'd personally be using a ksh or bash shell so you can do fancier stuff Smilie

Last edited by lavascript; 04-28-2009 at 08:14 AM..
# 7  
Old 04-29-2009
Bug

Hello,

Thank you for the help.

I definitely have to get a value returned into my var from the function else I'll run into the world of globals which I wish to avoid.

I would like to know if using "kill" is actually advised or if it has any side effects as am okay with checking the return status of the function in my parent script if kill is not the good way to go.

Thank You

Regards,
HKansal
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. UNIX for Beginners Questions & Answers

Function needs to be called based on its first character in a supplied string

Hi All, I want to write a bash script in which a function needs to be called based on its first character in a supplied string. eg function "j" should be called when "jab" or "jgh" or "j" .... etc is hit. I have used complete -F in below script, however here function is invoked... (1 Reply)
Discussion started by: temp.sha
1 Replies

3. Shell Programming and Scripting

Function getting called recursively

I have the below script which creates a directory or simply terminates without throwing error (exit 1) incase the directory exists. bash-4.1$ vi mdir.sh #!/bin/bash -e shopt -s expand_aliases alias mkdir=mkdir_s mkdir_s(){ if ]; then echo " directory EXISTS " return else echo "... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. UNIX for Dummies Questions & Answers

Problems with "exit" called from function in bourne script

Hi everyone. #!/sbin/sh EXITING() { umount /FOLDER rm -Rf /FOLDER echo "EXIT" exit 0 } EXITING echo "OK" (8 Replies)
Discussion started by: vacadepollo
8 Replies

5. UNIX for Dummies Questions & Answers

shell program- how many times a function is called

We have a program source C and is required to indicate how many times each function is called from the C program. also print the line number where there is a call. I've tried something like this: #!/bin/sh for i in $*;do if ! then echo $i is not a C file. else echo $i... (0 Replies)
Discussion started by: oana06
0 Replies

6. UNIX for Advanced & Expert Users

Function not called when no arguments is passed

Hi Guys, I am trying to pass arguments to the script i am wrinting. When no argument is passed or wrong argument is passed, the script needs to output the way it needs to be called and exit. Currently, when no arguments is passed, it is not getting exited but goes on assuming those... (3 Replies)
Discussion started by: mac4rfree
3 Replies

7. Shell Programming and Scripting

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,... (3 Replies)
Discussion started by: newbie_01
3 Replies

8. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

10. Programming

How to get exit value of an executable that gets called from function?

How to get exit value of an executable that gets called from function? I have an executable called “myexec” which returns 0 on success and different values for different fail scenarios. I need to call this (myexec) executable from “myprog()” function of other executable and get the exit value... (1 Reply)
Discussion started by: sureshreddi_ps
1 Replies
Login or Register to Ask a Question