In bash, is there an equivalent of die “error msg”

前端 未结 5 834
面向向阳花
面向向阳花 2020-12-04 15:31

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: <

5条回答
  •  感动是毒
    2020-12-04 15:48

    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"

提交回复
热议问题