Operating System - HP-UX
1847123 Members
5577 Online
110263 Solutions
New Discussion

return $error o exit $error.

 
SOLVED
Go to solution
Manuales
Super Advisor

return $error o exit $error.

Hi ..
could you let me know how can i use command "return $error o exit $error " into a shell , korn shell, c shell, etc ..

Please let me know ...

Regards.
3 REPLIES 3
Marco A.
Esteemed Contributor
Solution

Re: return $error o exit $error.

Actually I thing that you are talking about scripts , because I have never heard about a command like that.
Into a scrito you can use them, for return values and exit the script with determine exit value.

A little example for that could be the following...

sig = hpuxtobsdsig(SCARG(uap, signo));
if (sig <= 0 || sig >= NSIG || sig == SIGKILL || sig == SIGSTOP)
return (EINVAL);

Marco
Just unplug and plug in again ....
James R. Ferguson
Acclaimed Contributor

Re: return $error o exit $error.

Hi Manuales:

One uses 'return' with functions (subroutines) to exit the function or subroutine and provide a return value to the point where the function was called.

One uses 'exit' to immediately stop execution of a process and provide a return value to the caller. In the case of a script, the shell (parent) can interrogate the return code as the caller.

In general, a return code of zero (0) means success and non-zero means failure.

You can use 'return' and 'exit' like this Posix shell:

# cat ./mysh
#!/usr/bin/sh
function I_am_Good
{
return 0
}
function I_am_Bad
{
return 1
}

I_am_Good
RVAL=$?
[ "${RVAL}" = 0 ] && echo "Good is OK" || echo "Good Failed to be good"

I_am_Bad
RVAL=$?
[ "${RVAL}" = 0 ] && echo "Bad is OK" || echo "Bad Failed to be good"

exit 0

...run as:

# ./mysh; echo $?

The return code seen in your shell is zero (0) since the last statement as 'exit 0'.

By the way, you never evaluated responses to your questions here:

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=1115971

Regards!

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

Re: return $error o exit $error.

When called from the main body of a shell script, return and exit have the same effect -- they both exit the shell. When return is called from within a shell function, the function itself is exited but the calling shell continues to execute. On the contrary, when exit is called from withing a function or anywhere else with a script, the script terminates immediately. The exit status of the function or process is the value of the argument given to return or exit.

This really holds true for all shells, Perl, or C/C++ code.

By convention, an exit of 0 indicates success and any other value indicates failure. Also, by convention, you should make an effort to return the value that corresponds to what errno would have been set to had this been a system call. However, the actual exit status of a process or the return value of a function or subroutines means no more and no less than what the programmer/developer intended it to mean.

If it ain't broke, I can fix that.