zsh-users
 help / color / mirror / code / Atom feed
* Idiom for booleans
@ 2011-08-13  0:24 Micah Elliott
  2011-08-13  3:09 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Micah Elliott @ 2011-08-13  0:24 UTC (permalink / raw)
  To: zsh-users

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.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiom for booleans
  2011-08-13  0:24 Idiom for booleans Micah Elliott
@ 2011-08-13  3:09 ` Bart Schaefer
  2011-08-13  5:26   ` Micah Elliott
       [not found]   ` <CAELbujqBkE_zUG9M2+DS4mv=TxxKT=Rq4vH2fhXsbJ235B4nAA__26422.7002081045$1313213450$gmane$org@mail.gmail.com>
  0 siblings, 2 replies; 6+ messages in thread
From: Bart Schaefer @ 2011-08-13  3:09 UTC (permalink / raw)
  To: zsh-users

On Aug 12,  5:24pm, Micah Elliott wrote:
}
}   % (( 0 ))
}   ((: command not found
}   % (( 1 ))  # ok

This has to be something in your configuration.  Try it with zsh -f ?

torch% (( 0 ))
torch% ((
math> ))
torch% (( 0 )) || echo nope
nope
torch% 

This one I'm not sure is intentional:

torch% (( )) || echo oops
oops
torch% 

} 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.

Yes, that's a common usage.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiom for booleans
  2011-08-13  3:09 ` Bart Schaefer
@ 2011-08-13  5:26   ` Micah Elliott
  2011-08-15  1:11     ` Bart Schaefer
       [not found]   ` <CAELbujqBkE_zUG9M2+DS4mv=TxxKT=Rq4vH2fhXsbJ235B4nAA__26422.7002081045$1313213450$gmane$org@mail.gmail.com>
  1 sibling, 1 reply; 6+ messages in thread
From: Micah Elliott @ 2011-08-13  5:26 UTC (permalink / raw)
  To: Bart Schaefer, zsh-users

> On Aug 12,  5:24pm, Micah Elliott wrote:
> }
> }   % (( 0 ))
> }   ((: command not found

> This has to be something in your configuration.  Try it with zsh -f ?

Sweet relief! Thanks Bart!!

Disabling my setup with -f did the trick. Which led me on a goosechase
ending in this line in my setup:

  . /etc/zsh_command_not_found

That's a pretty handy utility (that I would expect to be popular), but
for now I'm going to live without it. I see it's a problem on the last
few Ubuntu releases, and I can't believe other people aren't seeing
the problem.

So when I do:

  % (( ))
  ((: command not found

Which ends up about the same as (except zsh was never actually complaining!):

  % dfjdkjfkdjf
  zsh: command not found: dfjdkjfkdjf
  dfjdkjfkdjf: command not found

It eventually ends up calling:

  /usr/bin/python /usr/lib/command-not-found -- "(("

So the buggy (or just bash-aware) python utility isn't going to work with zsh.

I'm also happy to see that by disabling command-not-found I can now do
this without funny "command not found" messages:

  % foo=bar ls not_a_file  # with command-not-found enabled
  ls: cannot access not_a_file: No such file or directory
  foo=bar: command not found  <-- UGH!!

I'll plan to file bugs on these for zsh. http://goo.gl/alnwN

-- 
twitter:@Membean  |  email:Micah@Membean.com  |  http://Membean.com
Remember your words with Membean! Three free days of learning!


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiom for booleans
       [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
  0 siblings, 0 replies; 6+ messages in thread
From: Štěpán Němec @ 2011-08-13 10:37 UTC (permalink / raw)
  To: Micah Elliott; +Cc: Bart Schaefer, zsh-users

On Sat, 13 Aug 2011 07:26:53 +0200
Micah Elliott wrote:

> So the buggy (or just bash-aware) python utility isn't going to work
> with zsh.

FWIW, I've been using this (put in my .zshrc), and never had any
problems (the (()) thing works as expected):

# from /etc/bash.bashrc, handle -> handler
if [ -x /usr/lib/command-not-found ]; then
  function command_not_found_handler {
  # check because c-n-f could've been removed in the meantime
  if [ -x /usr/lib/command-not-found ]; then
    /usr/bin/python /usr/lib/command-not-found -- $1
    return $?
  else
    return 127
  fi
}
fi

-- 
Štěpán


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiom for booleans
  2011-08-13  5:26   ` Micah Elliott
@ 2011-08-15  1:11     ` Bart Schaefer
  2011-08-15  8:28       ` Julien Nicoulaud
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2011-08-15  1:11 UTC (permalink / raw)
  To: zsh-users

On Aug 12, 10:26pm, Micah Elliott wrote:
} Subject: Re: Idiom for booleans
}
} > On Aug 12,  5:24pm, Micah Elliott wrote:
} > }
} > }   % (( 0 ))
} > }   ((: command not found
} 
} > This has to be something in your configuration.  Try it with zsh -f ?
} 
}   . /etc/zsh_command_not_found
} 
} That's a pretty handy utility (that I would expect to be popular), but
} for now I'm going to live without it. I see it's a problem on the last
} few Ubuntu releases, and I can't believe other people aren't seeing
} the problem.

I've had a Ubuntu laptop for a couple of years now and never noticed
this utility (not that I'd have used it if I had) so it's not all that
surprising to me that it isn't a widespread problem.

Ironically, the very first line of the precmd function defined by
/etc/zsh_command_not_found begins with

   (($?)) && ...

Obviously this implementation predates zsh introducing its own hook for
command-not-found.  It could have been done correctly, though, by the
simple expedient of

   (($? == 127)) && ...


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Idiom for booleans
  2011-08-15  1:11     ` Bart Schaefer
@ 2011-08-15  8:28       ` Julien Nicoulaud
  0 siblings, 0 replies; 6+ messages in thread
From: Julien Nicoulaud @ 2011-08-15  8:28 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1387 bytes --]

Thanks, it works perfectly now ! (Also, Ctrl+C on an empty buffer did output
a "command not found" message with the previous command run...)

I attached a branch to Micah's bug:
https://bugs.launchpad.net/ubuntu/+source/command-not-found/+bug/624565

2011/8/15 Bart Schaefer <schaefer@brasslantern.com>

> On Aug 12, 10:26pm, Micah Elliott wrote:
> } Subject: Re: Idiom for booleans
> }
> } > On Aug 12,  5:24pm, Micah Elliott wrote:
> } > }
> } > }   % (( 0 ))
> } > }   ((: command not found
> }
> } > This has to be something in your configuration.  Try it with zsh -f ?
> }
> }   . /etc/zsh_command_not_found
> }
> } That's a pretty handy utility (that I would expect to be popular), but
> } for now I'm going to live without it. I see it's a problem on the last
> } few Ubuntu releases, and I can't believe other people aren't seeing
> } the problem.
>
> I've had a Ubuntu laptop for a couple of years now and never noticed
> this utility (not that I'd have used it if I had) so it's not all that
> surprising to me that it isn't a widespread problem.
>
> Ironically, the very first line of the precmd function defined by
> /etc/zsh_command_not_found begins with
>
>   (($?)) && ...
>
> Obviously this implementation predates zsh introducing its own hook for
> command-not-found.  It could have been done correctly, though, by the
> simple expedient of
>
>   (($? == 127)) && ...
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2011-08-15  8:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-13  0:24 Idiom for booleans Micah Elliott
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

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).