在bash中,是否有相当于“错误消息”的东西?

发布于 2024-12-11 06:07:16 字数 137 浏览 0 评论 0原文

在 Perl 中,您可以使用 die "some msg" 退出并显示错误消息。 bash 中是否有等效的单个命令?现在,我正在使用命令来实现此目的:echo "some msg" &&出口1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酷遇一生 2024-12-18 06:07:16

你可以很容易地自己推出:

die() { echo "$*" 1>&2 ; exit 1; }
...
die "Kaboom"

You can roll your own easily enough:

die() { echo "$*" 1>&2 ; exit 1; }
...
die "Kaboom"
紅太極 2024-12-18 06:07:16

这是我正在使用的。它太小了,无法放入库中,所以我一定已经输入了数百次......

warn () {
    echo "$0:" "$@" >&2
}
die () {
    rc=$1
    shift
    warn "$@"
    exit $rc
}

用法:die 127“语法错误”

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 "$0:" "$@" >&2
}
die () {
    rc=$1
    shift
    warn "$@"
    exit $rc
}

Usage: die 127 "Syntax error"

丑丑阿 2024-12-18 06:07:16

这是一个与 Perl 的“die”非常接近的函数(但带有函数名称):

function die
{
    local message=$1
    [ -z "$message" ] && message="Died"
    echo "$message at ${BASH_SOURCE[1]}:${FUNCNAME[1]} line ${BASH_LINENO[0]}." >&2
    exit 1
}

如果内置函数失败,bash 的死亡方式(带有函数名称)

function die
{
    local message=$1
    [ -z "$message" ] && message="Died"
    echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${FUNCNAME[1]}: $message." >&2
    exit 1
}

因此,Bash 将所有需要的信息保存在几个环境变量中:

  • LINENO -当前执行的行号
  • FUNCNAME - 函数的调用堆栈,第一个元素(索引 0)是当前函数,第二个元素(索引 1)是调用当前函数的函数
  • BASH_LINENO - 行号的调用堆栈,其中调用了相应的 FUNCNAME
  • BASH_SOURCE - 源文件数组,存储相应的 FUNCNAME

This is a very close function to perl's "die" (but with function name):

function die
{
    local message=$1
    [ -z "$message" ] && message="Died"
    echo "$message at ${BASH_SOURCE[1]}:${FUNCNAME[1]} line ${BASH_LINENO[0]}." >&2
    exit 1
}

And bash way of dying if built-in function is failed (with function name)

function die
{
    local message=$1
    [ -z "$message" ] && message="Died"
    echo "${BASH_SOURCE[1]}: line ${BASH_LINENO[0]}: ${FUNCNAME[1]}: $message." >&2
    exit 1
}

So, Bash is keeping all needed info in several environment variables:

  • 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
┼── 2024-12-18 06:07:16

是的,你就是这么做的。

您可以使用分号或换行符而不是 &&,因为无论 echo 是否成功,您都想退出(尽管我不确定什么会导致它失败)。

在 shell 中编程意味着使用大量的小命令(一些内置命令,一些小程序)来很好地完成一件事,并将它们与文件重定向、退出代码逻辑和其他粘合剂连接起来。

如果您习惯使用函数或方法完成所有操作的语言,这可能看起来很奇怪,但您已经习惯了。

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).

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.

It may seem weird if you're used to languages where everything is done using functions or methods, but you get used to it.

迷离° 2024-12-18 06:07:16
# 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 $1 in
      [0-9]) ExitStatus="$1"; shift 1;;
  esac
    Msg="$*"
    test "$ExitStatus" = "0" || Msg=" ERROR: $Msg : $@"
    wlog " $Msg"
    exit $ExitStatus
}
#eof function Exit
# 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 $1 in
      [0-9]) ExitStatus="$1"; shift 1;;
  esac
    Msg="$*"
    test "$ExitStatus" = "0" || Msg=" ERROR: $Msg : $@"
    wlog " $Msg"
    exit $ExitStatus
}
#eof function Exit
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文