* Catch `setopt nounset`
@ 2015-04-09 16:30 Thorsten Kampe
2015-04-09 18:42 ` Bart Schaefer
0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Kampe @ 2015-04-09 16:30 UTC (permalink / raw)
To: zsh-users
Hi,
I'd to catch the call of a parameter that does not exist with `setopt
nounset`.
```
setopt nounset
testfunc() {
printf "$DOESNOTEXIST\n"
}
if testfunc
then
printf "no error\n"
else
printf "error\n"
fi
```
This does not work. Neither the then nor else clause are entered. 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?
```
if result=$(testfunc)
then
printf "no error\n"
else
printf "error\n"
fi
```
Thorsten
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Catch `setopt nounset`
2015-04-09 16:30 Catch `setopt nounset` Thorsten Kampe
@ 2015-04-09 18:42 ` Bart Schaefer
2015-04-10 1:11 ` Thorsten Kampe
0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2015-04-09 18:42 UTC (permalink / raw)
To: zsh-users
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.
You could get the same result with
if ( testfunc )
then
printf "no error\n"
else
printf "error\n"
fi
but of course that also suppresses all other side-effects of "testfunc",
which the "always" construct will not.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Catch `setopt nounset`
2015-04-09 18:42 ` Bart Schaefer
@ 2015-04-10 1:11 ` Thorsten Kampe
0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Kampe @ 2015-04-10 1:11 UTC (permalink / raw)
To: zsh-users
* 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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-04-10 1:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-09 16:30 Catch `setopt nounset` Thorsten Kampe
2015-04-09 18:42 ` Bart Schaefer
2015-04-10 1:11 ` Thorsten Kampe
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).