Is it bad practise to exit in function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is it bad practise to exit in function?
# 1  
Old 11-27-2009
Is it bad practise to exit in function?

Question is title, I don't understand why all examples I am reading return constants error values instead of just exiting under certain conditions... then back in main script test return value and exit if true, to me seems like a lot of extra typing and test conditions which can make for bulky and buggy code...
this is a test I just did, that shows you can exit from functions.... so I don't really understand what is wrong with the code below.

file: xxx
Code:
# function to exit
tstexit()
{
    echo "exiting now!"
    exit 1
}

file: zzz
Code:
#test for exit in function
. xxx
tstexit
echo "this is beyond the function will it echo?"
exit 0

output:
Code:
exiting now!


Last edited by gcampton; 11-27-2009 at 10:10 AM..
# 2  
Old 11-27-2009
It depends on your needs if you want to exit immediately from the function or want some processing of return codes 1st as kind of summary and central processing of these. I think that's totally up to you.
# 3  
Old 11-27-2009
See The Rule of Repair in the 'Art of Unix Programming' by Eric S Raymond

here:
Basics of the Unix Philosophy
# 4  
Old 11-27-2009
Quote:
Originally Posted by jim mcnamara
See The Rule of Repair in the 'Art of Unix Programming' by Eric S Raymond

here:
Basics of the Unix Philosophy

So going by that, I should be exiting in the function anyway rather than carrying on returning values back to main script to exit from there...

basically the examples have for instance, test on read/write file condition:
errors:
Code:
NOERROR=0
FILENOEXIST=1
FILENOREAD=3
FILELOCKED=9

readonly NOERROR FILENOEXIST FILENOREAD FILELOCKED

fnctns:
Code:
function chkread
{
    if [ ! -w $file ]
    then
        return $NOREAD
    fi
}

main script:
Code:
.fnctns

chkread
stat=$?

if [ "$stat" -eq "$NOREAD" ]
then
    echo "Unable to read/write, check permissions"
    echo  $usage
    exit 1
fi

seems a bit silly when you can have it just say in functions;

fnctns:
Code:
function chkread
{
    if [ ! -w $file ]
    then
        echo "Unable to read/write, check permissions"
        echo  $usage
        exit 1
    fi
}

main script:
Code:
.fnctns

chkread

Thanks for the link.

Last edited by gcampton; 11-27-2009 at 11:01 AM..
# 5  
Old 11-27-2009
IMO

ckread means: check to see if I can read a file

Code:
if [ ! -w $file ]

does not check to see if a file is readable or not.
# 6  
Old 11-27-2009
slip.... -r for read, -w for write, my script is testing just about every test possible, however when posting I was typing quick and only giving basic examples.

Last edited by gcampton; 11-27-2009 at 07:35 PM..
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. Programming

exit function doubts

At many places in program I see exit() being called with 2 passed as parameter to it. exit(2); What does this 2 stands for? Can I get a list all such parameters to exit? (6 Replies)
Discussion started by: rupeshkp728
6 Replies

3. Shell Programming and Scripting

Replacement of exit function

I am reading multiple numbers from user and getting its respective string value from the One.txt.But while putting this text value to the two.txt file , i want to check that if this string already there in the file , if the string already exists in the two.txt file .....the string will be ignored.... (1 Reply)
Discussion started by: jalpasoni
1 Replies

4. Shell Programming and Scripting

After exit from function it should not call other function

Below is my script that is function properly per my conditions but I am facing one problem here that is when one function fails then Iy should not check other functions but it calls the other function too So anyone can help me how could i achieve this? iNOUT i AM GIVING TO THE... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

5. UNIX for Advanced & Expert Users

Cannot exit from a function?

Hi, Is there a way to exit from a subcommand, which is a function in my example below? #!/bin/ksh function exitFunction { if ]; then echo "success" elif ]; then echo "failed" exit 1 # the exit problem fi exit 0 } ... (2 Replies)
Discussion started by: chstr_14
2 Replies

6. Shell Programming and Scripting

Exit from function

Hi, I just want to exit from function if some condition doesnt meet then control should go back to main program and start running from where it left.. When i used "exit" inside the function, its simply exited from entire script and it didnt run anymore.. Any idea how to do this.. Thanks... (3 Replies)
Discussion started by: nram_krishna@ya
3 Replies

7. Shell Programming and Scripting

exit from function

Hi all, My kshell code is not working, when I use a function to return something. But when I use the same function as without returning any values, it is working. Pls help me here. Code1 : func1 () { y=`echo $x | grep XXX| cut -f 2 -d ' '` if ; then exit 100 ... (2 Replies)
Discussion started by: poova
2 Replies

8. UNIX for Dummies Questions & Answers

EXIT from a Program not from the function..

Hi, I want a command in unix shell script which will exit my whole program, not only from the function it's using. For e.g: $1 == "m_who" && $4 == "Extrnl Vendor" { print "You don have access" exit (0); } If this error is showing on the screen, then nothing should not... (9 Replies)
Discussion started by: ronix007
9 Replies

9. Shell Programming and Scripting

exit shell script from function

Hi all, I have tried to put a exit 0 statement within a function I have created in the shell script but it doesn't seem to exit out of the script? Can someone tell me why? And is there any other way to exit out of the script from within a function? Thanks! (1 Reply)
Discussion started by: nvrh7
1 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