Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: a better way to 'die'?



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