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: Eric Blake
Subject: Re: [Help-bash] Something like die from perl in bash?
Date: Tue, 28 Jan 2014 08:49:47 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0

On 01/28/2014 08:40 AM, Peng Yu wrote:
> 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?

Yes

> 
> prog_to_fail || exit $? # how to print an error message as well exit
> with the last status code?

fail () {
  echo your message here
  exit $1
}
prog_to_fail || fail $?

or even

fail() {
  st=$?
  echo your message here
  exit $st
}
prog_to_fail || fail

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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