Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: a better way to 'die'?
- X-seq: zsh-users 16421
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: TJ Luoma <tj@xxxxxx>
- Subject: Re: a better way to 'die'?
- Date: Sat, 24 Sep 2011 20:47:02 +0200
- Cc: Zsh Users <zsh-users@xxxxxxx>
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=TdgIZ6qQrsS0o77i+P4HtmRIv8uRyUU9RUGaGUkRcNI=; b=JMmQbdquXkzo6eRxzfuqT6dqfC9CVLuS+zqT9Mv0rmBETMjLffN+Z7on6BO7MfCsTI u9BdZEs09d7SpoNVIGFW20ZCOoukapi1/nxUTEaQiY0WrYspU9hLFbL1ROpsnupg2KPx iiyBps4kR1dg1oMisiJz1xV+Rfwhg/8Tn2vvA=
- In-reply-to: <CADjGqHsFmUL=X6oWktW+X2eNQjQpPVyPCZ1htX8hSY-xRsBPsA@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <CADjGqHsFmUL=X6oWktW+X2eNQjQpPVyPCZ1htX8hSY-xRsBPsA@mail.gmail.com>
On 24 September 2011 20:09, TJ Luoma <tj@xxxxxx> 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
Messages sorted by:
Reverse Date,
Date,
Thread,
Author