View previous topic :: View next topic |
Author |
Message |
dman777 Veteran
Joined: 10 Jan 2007 Posts: 1004
|
Posted: Tue Sep 08, 2009 8:43 am Post subject: how do i implement exit status in a script? |
|
|
I've been told that it is better to have my bash script give the exit status when done. To me this seems redudant since the kernel will do this anyways. But, if I did implement exit status when my script is done, how would I do this? Don't I need to make a test condition for exit 0? What would I test for it to be true to exit 0? |
|
Back to top |
|
|
durian Guru
Joined: 16 Jul 2003 Posts: 312 Location: Margretetorp
|
Posted: Tue Sep 08, 2009 9:00 am Post subject: |
|
|
You can return a value (like "return 2") based on what your script has done. "return 0" if nothing went wrong, and "return 1" is some condition has occurred. If you don't return a value explicitly, your script will return the return value of the last function called, like you said.
-peter |
|
Back to top |
|
|
dman777 Veteran
Joined: 10 Jan 2007 Posts: 1004
|
Posted: Tue Sep 08, 2009 9:07 am Post subject: |
|
|
durian wrote: | ... "return 0" if nothing went wrong, and "return 1" is some condition has occurred.
-peter |
this is where i am stuck at. for the whole script, which calls various functions i wrote, at the end of the script how do I "return 0" if nothing went wrong, and "return 1" is some condition has occurred? meaning...based on what test condition? |
|
Back to top |
|
|
durian Guru
Joined: 16 Jul 2003 Posts: 312 Location: Margretetorp
|
Posted: Tue Sep 08, 2009 9:26 am Post subject: |
|
|
In that case it would depend on the exit codes of the functions you call. After each function, you must check the result (which will be in the $? variable after a function call), and return that if it is not zero. Or keep count until the end, and then return something based on the intermediate results.
(edit mistakes). I found an example:
Code: |
if [ $? -eq 0 ]; then
echo "Previous command ran succesfully."
fi
|
($? returns the exit value of the previous command)
-peter |
|
Back to top |
|
|
dman777 Veteran
Joined: 10 Jan 2007 Posts: 1004
|
Posted: Tue Sep 08, 2009 10:30 am Post subject: |
|
|
see, this is one of the areas i am having a problem with. the $? will only return the exit status of the last command, not the last function. correct? |
|
Back to top |
|
|
Nerevar l33t
Joined: 31 May 2008 Posts: 720
|
Posted: Tue Sep 08, 2009 11:35 am Post subject: |
|
|
Code: | #!/bin/sh
foo() {
return 2
}
#status=`foo`
exit `foo` |
|
|
Back to top |
|
|
widremann Veteran
Joined: 14 Mar 2005 Posts: 1314
|
Posted: Tue Sep 08, 2009 2:25 pm Post subject: |
|
|
I am very confused as to what you are trying to accomplish here.
If you want to exit with a status, just do "exit 0" (or "exit 1" or whatever). As someone else pointed out, $? has the exit status of the last command or function. You can store this value in a variable for later use, if you really need to know the exit status of every function. Usually, if a function or command fails and that failure prevents the script from continuing to execute properly, you should go ahead and exit, so you don't really need to hold on to the value. You can just do exit $?, or save it in a variable, echo some error messages and then exit $SAVED. |
|
Back to top |
|
|
dman777 Veteran
Joined: 10 Jan 2007 Posts: 1004
|
Posted: Thu Sep 10, 2009 3:59 am Post subject: |
|
|
widremann wrote: | I am very confused as to what you are trying to accomplish here.
If you want to exit with a status, just do "exit 0" (or "exit 1" or whatever). As someone else pointed out, $? has the exit status of the last command or function. You can store this value in a variable for later use, if you really need to know the exit status of every function. Usually, if a function or command fails and that failure prevents the script from continuing to execute properly, you should go ahead and exit, so you don't really need to hold on to the value. You can just do exit $?, or save it in a variable, echo some error messages and then exit $SAVED. |
I am confused on what I need to accomplish here. I've been told that it is proper etique to have me script give the exit status at the end. so basically, all this entails is the very last line 'exit $?' ? |
|
Back to top |
|
|
Mike Hunt Watchman
Joined: 19 Jul 2009 Posts: 5287
|
Posted: Thu Sep 10, 2009 4:03 am Post subject: |
|
|
The last line is generally "exit" or "exit 0" or "exit $WHATEVER"
Take a look at Advanced Bash-Scripting Guide |
|
Back to top |
|
|
i92guboj Bodhisattva
Joined: 30 Nov 2004 Posts: 10315 Location: Córdoba (Spain)
|
Posted: Thu Sep 10, 2009 4:14 am Post subject: |
|
|
A program can have many exit points. If it was just as simple as putting "exit 0" as the last line it would be pointless.
For example, imagine a simple script that calls feh to set the wallpaper, $1 will be the patch to the chosen wallpaper, right? Just quick code.
Code: |
#!/bin/bash
TOOL=$(which feh)
if [ -z "$TOOL" ]; then
# feh has not been found, exit with error code 1
exit 1
fi
if [ ! -r "$1" ]; then
# file is not readable, has not been found, or whatever
exit 2
fi
# set the wallpaper, however still feh could fail
# who knows? wrong format, alien invasions....
# if feh fails, we exit with 3
feh --bg-scale "$1" || exit 3
# if we have reached this point then everything went ok
# so we exit with 0
exit 0
|
This is just a simple valid-for-nothing example, but I hope you get the general picture. This allows any program calling this one to know if it was successful, and if not, it makes possible to know WHAT the problem was. |
|
Back to top |
|
|
|