在 bash 中,是否有等价于 die "error msg"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7868818/
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
In bash, is there an equivalent of die "error msg"
提问by PJx
In perl, you can exit with an error msg with die "some msg"
. Is there an equivalent single command in bash? Right now, I'm achieving this using commands: echo "some msg" && exit 1
在 perl 中,您可以使用die "some msg"
. bash 中是否有等效的单个命令?现在,我正在使用命令来实现这一点:echo "some msg" && exit 1
回答by Keith Thompson
You can roll your own easily enough:
您可以轻松地推出自己的产品:
die() { echo "$*" 1>&2 ; exit 1; }
...
die "Kaboom"
回答by tripleee
Here's what I'm using. It's too small to put in a library so I must have typed it hundreds of times ...
这是我正在使用的。它太小而不能放入图书馆,所以我必须输入数百次......
warn () {
echo "function die
{
local message=
[ -z "$message" ] && message="Died"
echo "$message at ${BASH_SOURCE[1]}:${FUNCNAME[1]} line ${BASH_LINENO[0]}." >&2
exit 1
}
:" "$@" >&2
}
die () {
rc=
shift
warn "$@"
exit $rc
}
Usage: die 127 "Syntax error"
用法: die 127 "Syntax error"
回答by Sergey Irisov
This is a very close function to perl's "die" (but with function name):
这是一个非常接近 perl 的“die”的函数(但有函数名):
function die
{
local message=
[ -z "$message" ] && message="Died"
echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${FUNCNAME[1]}: $message." >&2
exit 1
}
And bash way of dying if build-in function is failed (with function name)
如果内置函数失败(带有函数名称),则 bash 会死掉
# echo pass params and print them to a log file
wlog(){
# check terminal if exists echo
test -t 1 && echo "`date +%Y.%m.%d-%H:%M:%S` [$$] $*"
# check LogFile and
test -z $LogFile || {
echo "`date +%Y.%m.%d-%H:%M:%S` [$$] $*" >> $LogFile
} #eof test
}
# eof function wlog
# exit with passed status and message
Exit(){
ExitStatus=0
case in
[0-9]) ExitStatus=""; shift 1;;
esac
Msg="$*"
test "$ExitStatus" = "0" || Msg=" ERROR: $Msg : $@"
wlog " $Msg"
exit $ExitStatus
}
#eof function Exit
So, Bash is keeping all needed info in several environment variables:
因此,Bash 将所有需要的信息保存在几个环境变量中:
- LINENO - current executed line number
- FUNCNAME - call stack of functions, first element (index 0) is current function, second (index 1) is function that called current function
- BASH_LINENO - call stack of line numbers, where corresponding FUNCNAME was called
- BASH_SOURCE - array of source file, where corresponfing FUNCNAME is stored
- LINENO - 当前执行的行号
- FUNCNAME - 函数调用栈,第一个元素(索引 0)是当前函数,第二个(索引 1)是调用当前函数的函数
- BASH_LINENO - 调用行号堆栈,其中调用了相应的 FUNCNAME
- BASH_SOURCE - 源文件数组,其中存储了相应的 FUNCNAME
回答by bonkydog
Yep, that's pretty much how you do it.
是的,这就是你的做法。
You might use a semicolon or newline instead of &&, since you want to exit whether or not echo succeeds (though I'm not sure what would make it fail).
您可以使用分号或换行符代替 &&,因为无论 echo 是否成功,您都想退出(尽管我不确定是什么导致它失败)。
Programming in a shell means using lots of little commands (some built-in commands, some tiny programs) that do one thing well and connecting them with file redirection, exit code logic and other glue.
在 shell 中编程意味着使用许多小命令(一些内置命令,一些小程序),可以很好地完成一件事,并将它们与文件重定向、退出代码逻辑和其他胶水连接起来。
It may seem weird if you're used to languages where everything is done using functions or methods, but you get used to it.
如果您习惯于使用函数或方法完成一切的语言,这可能看起来很奇怪,但您已经习惯了。