Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: globbing in conditional expressions

# d.s@xxxxxxxxxxxxxxxxxx / 2014-05-14 04:19:08 +0000:
> Bart Schaefer wrote on Tue, May 13, 2014 at 08:41:17 -0700:
> > On May 8, 10:19pm, Roman Neuhauser wrote:
> > }
> > } maybe a crazy idea...  how about something like [[ -m pattern ]] which
> > } would succeed iff pattern matched at least one path?  this could be
> > } somewhat more amenable to shortcircuiting.
> > 
> > Returning to this after a bit of a detour through [[ ... ]] expression
> > parsing:
> > 
> > You could define (via zmodload) an operator that applies filename
> > generation to its argument, but changes to the internals of globbing
> > would be needed to make a short-circuit happen.  Then those changes
> > would have to be exposed somehow so that the operator could use them.
> > 
> I've taken a shot at making those changes, see attached.

wonderful, thank you!

> > As I mentioned before, in the case of the match failing this would be
> > exactly as expensive as not short-circuiting.
> Right, so short-circuiting would only be useful for callers that want to
> know whether a pattern matches a large directory, but don't care about
> having a list of matching filenames.  Does anyone have such a use-case?

s/a large directory// and you have exactly my use case: i only care
about simple and succint code.  (lack of) improved efficiency was Bart's
concern.  my original use case was along these lines:

  if ! is-mount $dst; then
    if ! is-empty $dst; then
      printf "%s: garbage in %s\n" $0 $dst
      return 1
    run sudo mount --bind $src $dst

where is-empty is now forced to

  function is-empty # {{{
    local -a d
    (( $#d == 0 ))
  } # }}}

whereas i expected to be able to write sth like

  if ! is-mount $dst; then
    if [[ -m $dst/*(D) ]]; then


  if ! is-mount $dst; then
    if [[ -m $dst(/F) ]]; then

this piece of code deals with directories typically under 10 entries
so i don't care about the performance impact of shortcircuting.

if i cared about the matched filenames, the code would be structured
differently and proably wouldn't call for a condexpr at all.

i'm trying to think of a situation where [[ -m $pat ]] && mangle $REPLY
would be useful for something other than foot-shooting... any ideas?


Messages sorted by: Reverse Date, Date, Thread, Author