Bash函数中的返回语句和退出语句在退出代码方面有什么区别?
当前回答
简单来说(主要针对编程新手),我们可以说:
`return`: exits the function,
`exit()`: exits the program (called as process while running)
如果你观察一下,这是非常基本的,但是…
`return`: is the keyword
`exit()`: is the function
其他回答
我认为没有人真正完全回答了这个问题,因为他们没有描述这两者是如何使用的。我想我们知道exit会终止脚本,不管它在哪里被调用你也可以给它分配一个状态比如exit, exit 0, exit 7等等。这可以用来确定如果被另一个脚本调用,脚本是如何被迫停止的,等等。到此为止。
Return,当调用时,将返回指定的值来指示函数的行为,通常是1或0。例如:
#!/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"
}
像这样检查:
if isdirectory $1; then echo "is directory"; else echo "not a directory"; fi
或者像这样:
isdirectory || echo "not a directory"
在本例中,可以使用测试来指示是否找到了目录。注意,函数中不会执行return之后的任何内容。0在shell中为真,但false为1,这与其他编程语言不同。
有关函数的更多信息:从Bash函数返回值
注意:isdirectory函数仅用于教学目的。这不应该是你在真正的脚本中执行这样的选项
首先,return是一个关键字,exit是一个函数。
也就是说,这里有一个最简单的解释。
返回
它从函数中返回一个值。
exit
它退出或放弃当前shell。
OP的问题是: BASH函数中的返回语句和退出语句在退出代码方面有什么区别?
首先,需要做一些澄清:
A (return|exit) statement is not required to terminate execution of a (function|shell). A (function|shell) will terminate when it reaches the end of its code list, even with no (return|exit) statement. A (return|exit) statement is not required to pass a value back from a terminated (function|shell). Every process has a built-in variable $? which always has a numeric value. It is a special variable that cannot be set like "?=1", but it is set only in special ways (see below *). The value of $? after the last command to be executed in the (called function | sub shell) is the value that is passed back to the (function caller | parent shell). That is true whether the last command executed is ("return [n]"| "exit [n]") or plain ("return" or something else which happens to be the last command in the called function's code.
在上面的项目符号列表中,从"(x|y)"中选择,要么总是第一项,要么总是第二项,分别获得关于函数和return的语句,或者shell和exit的语句。
很明显,它们都使用了特殊变量$?在值终止后向上传递值。
*现在是$?可设置:
When a called function terminates and returns to its caller then $? in the caller will be equal to the final value of $? in the terminated function. When a parent shell implicitly or explicitly waits on a single sub shell and is released by termination of that sub shell, then $? in the parent shell will be equal to the final value of $? in the terminated sub shell. Some built-in functions can modify $? depending upon their result. But some don't. Built-in functions "return" and "exit", when followed by a numerical argument both set $? with their argument, and terminate execution.
值得注意的是,$?可以通过在子shell中调用exit来赋值,如下所示:
# (exit 259)
# echo $?
3
如果将Bash脚本转换为函数,则通常将退出N替换为返回N。调用该函数的代码将把返回值视为子进程的退出代码。
在函数中使用exit将强制整个脚本结束。
Return将导致当前函数超出作用域,而exit将导致脚本在调用它的地方结束。下面是一个示例程序来帮助解释这一点:
#!/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"
输出
$ ./test.sh
this is retfunc()
We are still here
this is exitfunc()