LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-27-2009, 01:14 PM   #1
Jude Terror
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Rep: Reputation: 0
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.
 
Old 01-27-2009, 02:07 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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
 
Old 01-27-2009, 02:55 PM   #3
Jude Terror
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by matthewg42 View Post
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.
 
Old 01-27-2009, 03:09 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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"
 
Old 01-27-2009, 03:10 PM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Quote:
Originally Posted by Jude Terror View Post
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...

Last edited by H_TeXMeX_H; 01-28-2009 at 12:54 PM.
 
Old 01-27-2009, 04:15 PM   #6
Jude Terror
LQ Newbie
 
Registered: Dec 2008
Posts: 27

Original Poster
Rep: Reputation: 0
Excellent. Thank you very much.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optional exit from loop, shell script RudraB Programming 2 07-17-2008 04:30 AM
what is the script which runs at the exit bash shell supersubu123 Linux - General 6 01-24-2007 01:25 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
passing parameters to functions in shell script kushalkoolwal Programming 1 09-28-2005 03:40 PM
shell script functions iNET_boss Linux - General 1 08-10-2005 05:41 PM


All times are GMT -5. The time now is 04:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration