Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
how do i implement exit status in a script?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
dman777
Veteran
Veteran


Joined: 10 Jan 2007
Posts: 1004

PostPosted: Tue Sep 08, 2009 8:43 am    Post subject: how do i implement exit status in a script? Reply with quote

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
View user's profile Send private message
durian
Guru
Guru


Joined: 16 Jul 2003
Posts: 312
Location: Margretetorp

PostPosted: Tue Sep 08, 2009 9:00 am    Post subject: Reply with quote

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
View user's profile Send private message
dman777
Veteran
Veteran


Joined: 10 Jan 2007
Posts: 1004

PostPosted: Tue Sep 08, 2009 9:07 am    Post subject: Reply with quote

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
View user's profile Send private message
durian
Guru
Guru


Joined: 16 Jul 2003
Posts: 312
Location: Margretetorp

PostPosted: Tue Sep 08, 2009 9:26 am    Post subject: Reply with quote

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
View user's profile Send private message
dman777
Veteran
Veteran


Joined: 10 Jan 2007
Posts: 1004

PostPosted: Tue Sep 08, 2009 10:30 am    Post subject: Reply with quote

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
View user's profile Send private message
Nerevar
l33t
l33t


Joined: 31 May 2008
Posts: 720

PostPosted: Tue Sep 08, 2009 11:35 am    Post subject: Reply with quote

Code:
#!/bin/sh

foo() {
    return 2
}

#status=`foo`
exit `foo`
Back to top
View user's profile Send private message
widremann
Veteran
Veteran


Joined: 14 Mar 2005
Posts: 1314

PostPosted: Tue Sep 08, 2009 2:25 pm    Post subject: Reply with quote

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
View user's profile Send private message
dman777
Veteran
Veteran


Joined: 10 Jan 2007
Posts: 1004

PostPosted: Thu Sep 10, 2009 3:59 am    Post subject: Reply with quote

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
View user's profile Send private message
Mike Hunt
Watchman
Watchman


Joined: 19 Jul 2009
Posts: 5287

PostPosted: Thu Sep 10, 2009 4:03 am    Post subject: Reply with quote

The last line is generally "exit" or "exit 0" or "exit $WHATEVER"

Take a look at Advanced Bash-Scripting Guide
Back to top
View user's profile Send private message
i92guboj
Bodhisattva
Bodhisattva


Joined: 30 Nov 2004
Posts: 10315
Location: Córdoba (Spain)

PostPosted: Thu Sep 10, 2009 4:14 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum