Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Catch `setopt nounset`
* Bart Schaefer (Thu, 9 Apr 2015 11:42:05 -0700)
>
> On Apr 9, 6:30pm, Thorsten Kampe wrote:
> }
> } I'd to catch the call of a parameter that does not exist with `setopt
> } nounset`.
>
> setopt nounset
> {
> print "$DOESNOTEXIST"
> } always {
> if (( TRY_BLOCK_ERROR ))
> then print "error"
> else print "no error"
> fi
> }
>
> } If I replace the `catch` statement with `if result=$(testfunc)` it
> } prints "error". What am I missing here? Why does the latter work but
> } not the former?
>
> "parameter not set" is a fatal error, so whatever the current shell is
> doing is stopped when that happens. $(testfunc) is run in a subshell,
> so the current shell proceeds even though the subshell died.
Interesting explanation about the fatal error, subshell and an
interesting solution as well.
Unfortunately, the code should run in bash and zsh if possible, so I
went for `printf "$DOESNOTEXIST="` which catches the "`unbound
variable`/`parameter not set`" fatal error but still generates a
normal error because of the printf.
Of course the `always` solution is more general, more readable, and
superior.
Thorsten
Messages sorted by:
Reverse Date,
Date,
Thread,
Author