保存返回码并在 bash 中返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14267165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
saving return code and returning it in bash
提问by Betlista
I'd like to do this in bash
我想在 bash 中做到这一点
#!/bin/bash
func(){
return 1;
}
e=func
echo some text
exit e
but I'm getting
但我得到
exit: func: numeric argument required
AFAIK variables in bash are without a type, how to "convert" it to int to satisfy requirement?
bash 中的 AFAIK 变量没有类型,如何将其“转换”为 int 以满足要求?
回答by fge
You need to add a $in front of a variable to "dereference" it. Also, you must do this:
您需要$在变量前添加 a以“取消引用”它。此外,您必须这样做:
func
e=$?
# some commands
exit $e
$?contains the return code of the last executed "command"
$?包含最后执行的“命令”的返回码
Doing e=funcsets string functo variable e.
做e=func将 string 设置func为 variable e。

