zsh-users
 help / color / mirror / code / Atom feed
* a better way to 'die'?
@ 2011-09-24 18:09 TJ Luoma
  2011-09-24 18:47 ` Mikael Magnusson
  0 siblings, 1 reply; 2+ messages in thread
From: TJ Luoma @ 2011-09-24 18:09 UTC (permalink / raw)
  To: Zsh Users

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?

TjL


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: a better way to 'die'?
  2011-09-24 18:09 a better way to 'die'? TJ Luoma
@ 2011-09-24 18:47 ` Mikael Magnusson
  0 siblings, 0 replies; 2+ messages in thread
From: Mikael Magnusson @ 2011-09-24 18:47 UTC (permalink / raw)
  To: TJ Luoma; +Cc: Zsh Users

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-09-24 18:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-24 18:09 a better way to 'die'? TJ Luoma
2011-09-24 18:47 ` Mikael Magnusson

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).