Bash函数中的返回语句和退出语句在退出代码方面有什么区别?


当前回答

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

其他回答

简单来说(主要针对编程新手),我们可以说:

    `return`: exits the function,
    `exit()`: exits the program (called as process while running)

如果你观察一下,这是非常基本的,但是…

    `return`: is the keyword
    `exit()`: is the function

首先,return是一个关键字,exit是一个函数。

也就是说,这里有一个最简单的解释。

返回

它从函数中返回一个值。

exit

它退出或放弃当前shell。

记住,函数是脚本内部的,通常使用return语句返回调用它们的地方。调用外部脚本完全是另一回事,脚本通常以退出语句结束。

“Bash函数中关于退出代码的返回语句和退出语句之间”的差异非常小。两者都返回一个状态,而不是本身的值。状态为0表示成功,而任何其他状态(1到255)表示失败。return语句将从调用它的地方返回到脚本,而exit语句将从遇到它的地方结束整个脚本。

return 0  # Returns to where the function was called.  $? contains 0 (success).

return 1  # Returns to where the function was called.  $? contains 1 (failure).

exit 0  # Exits the script completely.  $? contains 0 (success).

exit 1  # Exits the script completely.  $? contains 1 (failure).

如果函数只是结束而没有返回语句,则最后执行的命令的状态将作为状态代码返回(并将放置在$?)

记住,return和exit返回一个从0到255的状态码,可用$?你不能把任何东西塞进状态码(例如,返回“cat”);这是行不通的。但是,脚本可以通过使用状态码返回255个不同的失败原因。

您可以设置调用脚本中包含的变量,或者在函数中回显结果,并在调用脚本中使用命令替换;但是return和exit的目的是传递状态码,而不是像C这样的编程语言中所期望的值或计算结果。

Exit终止当前进程;不管有没有退出码,都应该把它看作一个系统,而不是一个程序函数。注意,当使用源时,exit将结束shell。但是,在运行时,它将退出脚本。 从函数返回,返回到调用后的指令,带或不带返回码。Return是可选的,在函数末尾是隐式的。Return只能在函数内部使用。

我想补充一点,在获取源代码的同时,不杀死shell而从函数中退出脚本并不容易。我认为,“测试”脚本上的示例更好:

#!/bin/bash
function die(){
   echo ${1:=Something terrible wrong happen}
   #... clean your trash
   exit 1
}

[ -f /whatever/ ] || die "whatever is not available"
# Now we can proceed
echo "continue"

做以下事情:

user$ ./test
Whatever is not available
user$

测试后,外壳会闭合。

user$ . ./test
Whatever is not available

只有测试将完成,并显示提示。

解决方案是将潜在的过程包含在(和)中:

#!/bin/bash
function die(){
   echo $(1:=Something terrible wrong happen)
   #... Clean your trash
   exit 1
}

( # Added        
    [ -f /whatever/ ] || die "whatever is not available"
    # Now we can proceed
    echo "continue"
) # Added

现在,在这两种情况下,只有test将退出。

有时,使用。或来源。

. a.sh

如果在a.s sh中包含了一个exit,它不仅会终止脚本,还会结束shell会话。

如果在a.h sh中包含一个返回值,它就会停止处理脚本。