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
        [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:

$ 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

Try giving exit with a number
exit 1

Change

echo `doThis "abcd"`

to

doThis "abcd"

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.


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 "

Hello,

Thank you for helping.

@devtakh:
the status code with exit does not help :(. 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:

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

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

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 :slight_smile:

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

Hello,

I am posting the final script created under the initial thread :here:

Thank You

Regards,
HKansal