From: Micah Elliott <micah@membean.com>
To: zsh-users@zsh.org
Subject: Idiom for booleans
Date: Fri, 12 Aug 2011 17:24:14 -0700 [thread overview]
Message-ID: <CAELbujo=KyJyzzo1vW+ydLVA=PddguguXkBr7yN3zvmOgmYFpQ@mail.gmail.com> (raw)
Hi! I'm trying to figure out if the $+ expansion, as in ${+param}, is
worth making use of. What idiom do you use for it? I could see it as a
boolean for testing flags. As in:
% verbosity=high myscript.zsh
...
(( $+verbosity )) && print chatty
chatty
% unset verbosity # uh-oh
% (( $+verbosity )) && print chatty # should this work?
((: command not found
I did see this idiom used in the Zsh Guide
(http://zsh.sourceforge.net/Guide/zshguide03.html as: (( OPTIND > 1 ))
&& shift $(( OPTIND - 1 )) ) -- is that a bug?
I suppose it boils down to understanding (explanation welcome here):
% (( 0 ))
((: command not found
% (( 1 )) # ok
Wrapping in "if" makes it work too, but just feels tedious:
% if (( 0 )); then print i say nothing; fi
----
I could make the boolean work by adding a fall-thru:
% (( $+verbosity )) && print chatty || :
but that feels rotten. Or I could use:
% [[ $+verbosity == 1 ]] && print chatty
but that doesn't seem any better than the more common and simpler:
% [[ -n $verbosity ]] && print chatty
So is there some concise idiomatic way of making use of $+ ? Or is
this last line the better way to make such tests?
--
twitter:@Membean | email:Micah@Membean.com | http://Membean.com
Remember your words with Membean. Three free days of learning! GO SIGN UP.
next reply other threads:[~2011-08-13 0:24 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-08-13 0:24 Micah Elliott [this message]
2011-08-13 3:09 ` Bart Schaefer
2011-08-13 5:26 ` Micah Elliott
2011-08-15 1:11 ` Bart Schaefer
2011-08-15 8:28 ` Julien Nicoulaud
[not found] ` <CAELbujqBkE_zUG9M2+DS4mv=TxxKT=Rq4vH2fhXsbJ235B4nAA__26422.7002081045$1313213450$gmane$org@mail.gmail.com>
2011-08-13 10:37 ` Štěpán Němec
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='CAELbujo=KyJyzzo1vW+ydLVA=PddguguXkBr7yN3zvmOgmYFpQ@mail.gmail.com' \
--to=micah@membean.com \
--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).