help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Something like die from perl in bash?


From: Geir Hauge
Subject: Re: [Help-bash] Something like die from perl in bash?
Date: Tue, 28 Jan 2014 17:07:24 +0100

2014/1/28 Peng Yu <address@hidden>

> Hi,
>
> "die" in perl can print an error message as well as set the return
> code of the perl script. I frequently need to print an error message
> and exit with the last error code. I'd like to make it one line. Is it
> possible in bash?
>
> prog_to_fail || exit $? # how to print an error message as well exit
> with the last status code?


Just write a die function. E.g.

die() {
    local status=$?
    printf >&2 '%s\n' "$1"
    exit "$status"
}

[[ -f $configfile ]] || die "Missing $configfile"

-- 
Geir Hauge


reply via email to

[Prev in Thread] Current Thread [Next in Thread]