From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9318 invoked by alias); 10 Apr 2015 01:12:03 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 20117 Received: (qmail 13396 invoked from network); 10 Apr 2015 01:11:51 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS autolearn=ham version=3.3.2 X-Injected-Via-Gmane: http://gmane.org/ To: zsh-users@zsh.org From: Thorsten Kampe Subject: Re: Catch `setopt nounset` Date: Fri, 10 Apr 2015 03:11:43 +0200 Message-ID: References: <150409114205.ZM25276@torch.brasslantern.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: p5de5aada.dip0.t-ipconnect.de User-Agent: MicroPlanet-Gravity/3.0.4 * 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