Difference between return and exit in BASH functions
是什么之间的区别return
和exit
的BASH功能相对于退出代码说法?
return
returns a value from a function. exit
abandons the current shell.
EDIT:
As per your edit of the question, regarding exit codes, return
has nothing to do with exit codes. Exit codes are intended for applications/scripts , not functions. So in this regard, the only keyword that sets the exit code of the script (the one that can be caught by the calling program using the $?
shell variable) is exit
.
EDIT 2:
My last statement referring exit
is causing some comments. It was made to differentiate return
and exit
for the understanding of the OP, and in fact, at any given point of a program/shell script, exit
is the only way of ending the script with an exit code to the calling process.
Every command executed in the shell produces a local "exit code": it sets the $?
variable to that code, and can be used with if
, &&
and other operators to conditionally execute other commands.
These exit codes (and the value of the $?
variable) are reset by each command execution.
Incidentally, the exit code of the last command executed by the script is used as the exit code of the script itself as seen by the calling process.
Finally, functions, when called, act as shell commands with respect to exit codes. The exit code of the function (within the function) is set by using return
. So when in a function return 0
is run, the function execution terminates, giving an exit code of 0.
return
will cause the current function to go out of scope, while exit
will cause the script to end at the point where it is called. Here is a sample program to help explain this:
#!/bin/bash
retfunc()
{
echo "this is retfunc()"
return 1
}
exitfunc()
{
echo "this is exitfunc()"
exit 1
}
retfunc
echo "We are still here"
exitfunc
echo "We will never see this"
Output
$ ./test.sh
this is retfunc()
We are still here
this is exitfunc()
I don't think anyone has really fully answered the question because they don't describe how the two are used. OK I think we know that exit kills the script, where ever it is called and you can assign a status to it as well such as exit or exit 0 or exit 7 and so forth. This can be used to determine how the script was forced to stop if called by another script etc. Enough on exit.
return when called will return the value specified to indicate the function's behavior, usually a 1 or a 0. For example:
#!/bin/bash
isdirectory() {
if [ -d "$1" ]
then
return 0
else
return 1
fi
echo "you will not see anything after the return like this text"
}
check like this:
if isdirectory $1; then echo "is directory"; else echo "not a directory"; fi
or like this:
isdirectory || echo "not a directory"
In this example, the test can be used to indicate if the directory was found. notice that anything after the return will not be executed in the function. 0 is true but false is 1 in the shell, different from other prog langs.
For more info on functions: http://www.linuxjournal.com/content/return-values-bash-functions
NOTE: The isdirectory function is for instructional purposes only. This should not be how you perform such an option in a real script.
链接地址: http://www.djcxy.com/p/57322.html上一篇: 将复制
下一篇: BASH函数中返回和退出之间的区别