zsh-users
 help / color / mirror / code / Atom feed
From: Mikael Magnusson <mikachu@gmail.com>
To: TJ Luoma <tj@luo.ma>
Cc: Zsh Users <zsh-users@zsh.org>
Subject: Re: a better way to 'die'?
Date: Sat, 24 Sep 2011 20:47:02 +0200	[thread overview]
Message-ID: <CAHYJk3RyaY0XvKQjQqZFKO_fFfbPnRACbAKc3wb887fQ-YfOPg@mail.gmail.com> (raw)
In-Reply-To: <CADjGqHsFmUL=X6oWktW+X2eNQjQpPVyPCZ1htX8hSY-xRsBPsA@mail.gmail.com>

On 24 September 2011 20:09, TJ Luoma <tj@luo.ma> wrote:
> I have a function 'die' in my .zshenv that looks like this:
>
> die () {
>
>        echo "$@"
>
>        if [ "$SHLVL" = "1" ]
>        then
>                return 1
>        else
>                exit 1
>        fi
> }
>
> The purpose is so I can do things like:
>
> [[ -e "/path/to/file.txt" ]] || die "Did not find /path/to/file.txt!"
>
> to easily quit a loop (using return 1) or a script (using exit 1)
> after echoing a message explaining where things broke down. (That
> seems better than just throwing an 'exit 1' or 'return 1' and leaving
> the user — usually me — to figure out where things failed.)
>
> Is there a better way to do this?

If you have a recent enough zsh, look at $zsh_eval_context[-1]

-- 
Mikael Magnusson


      reply	other threads:[~2011-09-24 18:47 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-24 18:09 TJ Luoma
2011-09-24 18:47 ` Mikael Magnusson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAHYJk3RyaY0XvKQjQqZFKO_fFfbPnRACbAKc3wb887fQ-YfOPg@mail.gmail.com \
    --to=mikachu@gmail.com \
    --cc=tj@luo.ma \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).