From: Roman Perepelitsa <roman.perepelitsa@gmail.com>
To: sergio <sergio@outerface.net>
Cc: zsh-users@zsh.org
Subject: Re: Arithmetic Zero
Date: Tue, 5 Mar 2024 08:26:19 +0100 [thread overview]
Message-ID: <CAN=4vMrMjUtk87sNQHRagM2TYyP9jDUJrskgia8wOiQDtTx94w@mail.gmail.com> (raw)
In-Reply-To: <47117922-e17f-44d1-8a2f-2cb4a9c53606@outerface.net>
On Tue, Mar 5, 2024 at 4:28 AM sergio <sergio@outerface.net> wrote:
>
> ```
> #!/bin/zsh -e
> (( test = 0 ))
> ```
>
> 2. How to do arithmetic evaluation properly in zsh sripts? postpend with
> || true
When I end up in a situation like this, I use this pattern:
(( expr, 1 ))
Although the more general patterns works here, too:
list || true
I never use `set -e`, a.k.a. `setopt err_exit`, on its own, because
its behavior within functions is counter to what I need. In fact, I
don't think I ever wanted this option to behave the way it does in
functions. Let me show what I mean.
set -e
false
launch-missiles
If you put these lines at the top level of a zsh script,
launch-missiles won't execute because the previous command (false)
fails. But if you put the same code in a function, this guarantee is
lost.
% zsh -fc '
foo() {
set -e
false
print launch missiles
}
if ! foo; then :; fi
foo || false
foo && true
print the end'
The output:
launch missiles
launch missiles
launch missiles
the end
This issue can be solved by turning on err_return together with err_exit.
foo() {
setopt local_options err_return err_exit
false
launch-missiles
}
Now, we can either handle errors from the function gracefully:
if ! foo; then
do-something
fi
Or we can let it take the default error handling action, which is to
exit the script:
foo
In no case will the missiles launch.
Roman.
prev parent reply other threads:[~2024-03-05 7:28 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-05 3:28 sergio
2024-03-05 4:23 ` Bart Schaefer
2024-03-05 5:02 ` Lawrence Velázquez
2024-03-05 7:09 ` Stephane Chazelas
2024-03-05 7:26 ` Roman Perepelitsa [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAN=4vMrMjUtk87sNQHRagM2TYyP9jDUJrskgia8wOiQDtTx94w@mail.gmail.com' \
--to=roman.perepelitsa@gmail.com \
--cc=sergio@outerface.net \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).