1838469 Members
2844 Online
110126 Solutions
New Discussion

exit on a script

 
SOLVED
Go to solution
someone_4
Honored Contributor

exit on a script

When and why and should you type exit at the end of a script ?

Thanks
Richard
10 REPLIES 10
Rainer von Bongartz
Honored Contributor

Re: exit on a script

passing a parameter with exit (exit 2 i.e.) you can check the exit starus from your script. useful if you want to check wheter your script dit an normal exit or aborted on an error condition .

He's a real UNIX Man, sitting in his UNIX LAN making all his UNIX plans for nobody ...
Ravi_8
Honored Contributor

Re: exit on a script

Hi,

after executing the script exit(1) just comes back to prompt without closing the file, where as exit(2) closes the file and comes out
never give up
someone_4
Honored Contributor

Re: exit on a script

so i should use exit(2) on every script?

Richard
Ravi_8
Honored Contributor

Re: exit on a script

Hi,
sorry for mistake, it's actually it is exit(0) and exit(1). yu can use exit(1) at the script end.
never give up
James R. Ferguson
Acclaimed Contributor
Solution

Re: exit on a script

Hi Richard:

First, it is good practice to exit a script with a defined return value. If you don't, the exit value will be the return value of the last command executed, by default.

Standards define three exit values, the remainder being a user's choice:

0 => command or process completed successfully
1 => command or process failed
2 => command or process had warnings

A good example of a command following these guidelines is 'grep'. An example of a command that deviates is 'diff'. Take a look at the man pages for both to see!

Thus, even these guidelines are not hard-and-fast.

Regards!

...JRF...
Thierry Poels_1
Honored Contributor

Re: exit on a script

hi,

in most cases you don't really need an exit statement at the end of your script. The shell process will terminate at the end of your script unless you have set "ignoreeof" option.
Exit is mostly used to exit from within your script at certain conditions: error, file not found, execute not required, .....
"Exit" or "Exit 0" is normally used to leave the script, and state that is ended normally.
Exit codes >= 1 are normally used to indicate some error has occured.
regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Bill Hassell
Honored Contributor

Re: exit on a script

There's one good reason to add exit (or exit 0) at the end of every script: It documents the true end of the file. If someone miscopies or truncates a script, there is no way to know whether all the lines are still there. By having both a comment and exit 0 such as:

# end of script
exit 0

then the reader will be assured that the end of the file is present.


Bill Hassell, sysadmin
James R. Ferguson
Acclaimed Contributor

Re: exit on a script

Hi Bill:

I can't resist saying "AMEN!". The notation of "end " is so desirable...on reports, in code... At least in languages like ALGOL and PASCAL we have the enforced "END" to delineate the outer block! ;-)

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: exit on a script

Hi Richard,

There is one more reason, when your script calls another scrip, there is a reliable means of checking the status.
e.g.
XXX=`somecommand.sh`
STAT=$?

Now $STAT is the exit status of somecommand.sh while $XXX contains its output.

This also should be extended to functions within your scripts.

somefunc()
{
FUNCSTAT=0
echo "This is somefunc output \c:"
while [ $# -ge 1 ]
do
echo "Arg ${1} \c"
shift
done
echo
return(${FUNCSTAT})
}

XXX=`somefunc 111 222`
STAT=$?

Here the same rules as above apply. Typically, you test the exit(return) status before ever doing anything with the script(function) output.

The last point is that if you do c programming the same rules apply: main() should also return a value.

Clay
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: exit on a script

Hi again Richard,

Looking over the earlier replies, it appears that ravi was not quite correct. There is no default behavior associated with exit(1) or exit(0). Simply follow the standard rule:
0 means good; anything else not so good.
If it ain't broke, I can fix that.