LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to properly exit functions in Shell Script? (https://www.linuxquestions.org/questions/programming-9/how-to-properly-exit-functions-in-shell-script-700259/)

Jude Terror 01-27-2009 12:14 PM

How to properly exit functions in Shell Script?
 
Is there a proper way to exit a function without returning to previous lines in that function?

For instance, I have a shell script with lots of functions, many of them called from within functions:

Code:

functionOne(){
functionTwo
functionThree
functionFour
functionFive
}

Then functionTwo will do something like:

get data from user
test data from user
if data is incorrect then go to another function called tryAgain

Code:

tryAgain(){
Echo "Do you want to try again?"
read yesno
case $yesno in
"yes") functionOne;;
"no") exit;;
esac

If the user chooses no, instead of terminating the program, it simply returns to the next step in functionOne. So if the tryAgain function is called in functionTwo, and the user chooses no, it simply moves on to functionThree instead of exiting. The same thing occurs if I replace exit with
Code:

kill $$
If the tryAgain function is called in functionFive, where there are nno more steps below it, then the exit command works.

Any idea what I'm doing wrong here? Is there a way to terminate the processing of a function?

I can post the actual script if needed, but it's really long, so I thought I'd paraphrase here.

Thanks a lot in advance for any assistance.

matthewg42 01-27-2009 01:07 PM

Use "return". I would also recommend indenting your code - makes it a lot more readable.

For example:

Code:

#!/bin/bash

f1 () {
    echo "We are in f1"
    return 0
    echo "This line should never execute"
}

f2 () {
    echo "We are in f2, about to call f1"
    f1
    echo "Back in f2, after calling f1"
    return
    echo "This line will not execute"
}

# now the functions are defined, you can call them.  We'll call f2

echo "About to call f2"
f2
echo "After calling f2, back in main flow"

The output should look like this:
Code:

About to call f2
We are in f2, about to call f1
We are in f1
Back in f2, after calling f1
After calling f2, back in main flow


Jude Terror 01-27-2009 01:55 PM

Quote:

Originally Posted by matthewg42 (Post 3423062)
Use "return". I would also recommend indenting your code - makes it a lot more readable.

For example:

Code:

#!/bin/bash

f1 () {
    echo "We are in f1"
    return 0
    echo "This line should never execute"
}

f2 () {
    echo "We are in f2, about to call f1"
    f1
    echo "Back in f2, after calling f1"
    return
    echo "This line will not execute"
}

# now the functions are defined, you can call them.  We'll call f2

echo "About to call f2"
f2
echo "After calling f2, back in main flow"

The output should look like this:
Code:

About to call f2
We are in f2, about to call f1
We are in f1
Back in f2, after calling f1
After calling f2, back in main flow


Thanks I'll try that out. Where does the 0 in return 0 end up? Is that the exit status of the function? Is there a way to assign that to a variable?

Thanks a lot for the help.

Oh, and yeah I do indent my code - I just didn't in that example because the tab button doesn't work here in the posting window.

matthewg42 01-27-2009 02:09 PM

The return value is assigned to $? after the function returns. It should be a positive integer, generally less than 127. 0 indicates success. See the EXIT STATUS section of the bash manual page for more information.

If you don't use a return in a function, the exit status of the last executed command is used instead.

For example:
Code:

f1 () {
    # do something here, then test to see if it worked
    if [ SOMETESTOFSUCCESSHERE ]; then
        return 0
    else
        return 1
    fi
}

if [ $? -ne 0 ]; then
    echo "WARNING, f1 didn't work"
fi

A quicker (but probably less readable to shell beginners) way of doing the if is like this:
Code:

f1 || echo "WARNING, f1 didn't work"

H_TeXMeX_H 01-27-2009 02:10 PM

Quote:

Originally Posted by Jude Terror (Post 3423125)
Thanks I'll try that out. Where does the 0 in return 0 end up? Is that the exit status of the function? Is there a way to assign that to a variable?

Yup ...

Code:

bash-3.1$ new() if test $1 == 1; then return 0; else return 1; fi
bash-3.1$ if new 1; then echo 2; fi
2
bash-3.1$ if new 2; then echo 2; fi
bash-3.1$

Same goes for the entire script, but using 'exit 0' or 'exit 1', etc...

Jude Terror 01-27-2009 03:15 PM

Excellent. Thank you very much.


All times are GMT -5. The time now is 09:17 PM.