zsh-users
 help / color / mirror / code / Atom feed
* [[ -f file* ]]
@ 2009-05-18  9:14 Atom Smasher
  2009-05-18  9:36 ` Matt Wozniski
  0 siblings, 1 reply; 5+ messages in thread
From: Atom Smasher @ 2009-05-18  9:14 UTC (permalink / raw)
  To: zsh-users

if i want to test for the presence of one or more files matching a certain 
pattern, it seems non-trivial to do it with the normal "test" or 
conditional expressions.

so far this seems like the best way to do it:
 	{ ls test* } 2> /dev/null | read -k 2 -u 0

if one or more files match the pattern "test*", read returns 0. if no 
files match the pattern, read returns >0.

is there a better way?

thanks...


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"Wars not make one great." -- Yoda


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

* Re: [[ -f file* ]]
  2009-05-18  9:14 [[ -f file* ]] Atom Smasher
@ 2009-05-18  9:36 ` Matt Wozniski
  2009-05-18  9:55   ` Peter Stephenson
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Matt Wozniski @ 2009-05-18  9:36 UTC (permalink / raw)
  To: zsh-users

On Mon, May 18, 2009 at 5:14 AM, Atom Smasher wrote:
> if i want to test for the presence of one or more files matching a certain
> pattern, it seems non-trivial to do it with the normal "test" or conditional
> expressions.
...
> is there a better way?

Well, off the top of my head, I can think of

[ -n "$(print -- test*(N))" ]

but even that seems inelegant; I'm sure someone can chime in with
something better.

~Matt


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

* Re: [[ -f file* ]]
  2009-05-18  9:36 ` Matt Wozniski
@ 2009-05-18  9:55   ` Peter Stephenson
  2009-05-18 11:35   ` Atom Smasher
  2009-05-18 11:40   ` Frank Terbeck
  2 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2009-05-18  9:55 UTC (permalink / raw)
  To: zsh-users

On Mon, 18 May 2009 05:36:18 -0400
Matt Wozniski <godlygeek@gmail.com> wrote:
> On Mon, May 18, 2009 at 5:14 AM, Atom Smasher wrote:
> > if i want to test for the presence of one or more files matching a certain
> > pattern, it seems non-trivial to do it with the normal "test" or conditional
> > expressions.
> ...
> > is there a better way?
> 
> Well, off the top of my head, I can think of
> 
> [ -n "$(print -- test*(N))" ]
> 
> but even that seems inelegant; I'm sure someone can chime in with
> something better.

has_matches() {
  (( $# > 0 ))
}

if has_matches test*(N); then
  # some matches
else
  # no matches
fi


You need to remember the (N).  There are ways around that.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


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

* Re: [[ -f file* ]]
  2009-05-18  9:36 ` Matt Wozniski
  2009-05-18  9:55   ` Peter Stephenson
@ 2009-05-18 11:35   ` Atom Smasher
  2009-05-18 11:40   ` Frank Terbeck
  2 siblings, 0 replies; 5+ messages in thread
From: Atom Smasher @ 2009-05-18 11:35 UTC (permalink / raw)
  To: zsh-users

On Mon, 18 May 2009, Matt Wozniski wrote:

> Well, off the top of my head, I can think of
>
> [ -n "$(print -- test*(N))" ]
>
> but even that seems inelegant; I'm sure someone can chime in with 
> something better.
=================

it's less inelegant than my original method ;)

and it works fine with '[[' conditional expressions... which, IIUC, is the 
"new and improved" version of test.


-- 
         ...atom

  ________________________
  http://atom.smasher.org/
  762A 3B98 A3C3 96C9 C6B7 582A B88D 52E4 D9F5 7808
  -------------------------------------------------

 	"In every respect, vegans appear to enjoy equal
 	 or better health in comparison to both vegetarians
 	 and non-vegetarians."
 		-- T. Colin Campbell,
 		PhD Professor of Nutrition, Cornell University


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

* Re: [[ -f file* ]]
  2009-05-18  9:36 ` Matt Wozniski
  2009-05-18  9:55   ` Peter Stephenson
  2009-05-18 11:35   ` Atom Smasher
@ 2009-05-18 11:40   ` Frank Terbeck
  2 siblings, 0 replies; 5+ messages in thread
From: Frank Terbeck @ 2009-05-18 11:40 UTC (permalink / raw)
  To: zsh-users

Matt Wozniski <godlygeek@gmail.com>:
> On Mon, May 18, 2009 at 5:14 AM, Atom Smasher wrote:
> > if i want to test for the presence of one or more files matching a certain
> > pattern, it seems non-trivial to do it with the normal "test" or conditional
> > expressions.
> ...
> > is there a better way?
> 
> Well, off the top of my head, I can think of
> 
> [ -n "$(print -- test*(N))" ]
> 
> but even that seems inelegant; I'm sure someone can chime in with
> something better.

How about this:

[snip]
set -- test*(N)
(( $# > 0 ))
[snap]

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925


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

end of thread, other threads:[~2009-05-18 11:42 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-18  9:14 [[ -f file* ]] Atom Smasher
2009-05-18  9:36 ` Matt Wozniski
2009-05-18  9:55   ` Peter Stephenson
2009-05-18 11:35   ` Atom Smasher
2009-05-18 11:40   ` Frank Terbeck

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