zsh-users
 help / color / mirror / code / Atom feed
* checking for file existence when I don't know the exact name
@ 2011-10-24 13:16 TJ Luoma
  2011-10-24 13:29 ` Jérémie Roquet
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: TJ Luoma @ 2011-10-24 13:16 UTC (permalink / raw)
  To: Zsh Users

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

I want to check to see if a file exists that matches this pattern:

OmniFocus-XXXXX.omnilicense

where XXXXX is some set of numbers of an unknown length.

I've been using

ls | egrep -q "OmniFocus.*\.omnilicense"

but it occurs to me that I might be able to use [[ -e ]]

However, I'm not sure what the syntax is. I tried:

if [[ -e "OmniFocus.*.omnilicense" ]]
then

{take action here}

fi

but that did not work. Do I have the syntax wrong or do I need to make sure
that some setting is enabled?

Thanks!

TjL

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

* Re: checking for file existence when I don't know the exact name
  2011-10-24 13:16 checking for file existence when I don't know the exact name TJ Luoma
@ 2011-10-24 13:29 ` Jérémie Roquet
  2011-10-24 14:25   ` TJ Luoma
  2011-10-24 13:31 ` Peter Stephenson
  2011-10-24 13:35 ` Stephane CHAZELAS
  2 siblings, 1 reply; 9+ messages in thread
From: Jérémie Roquet @ 2011-10-24 13:29 UTC (permalink / raw)
  To: Zsh Users; +Cc: TJ Luoma

Hi,

2011/10/24 TJ Luoma <luomat@gmail.com>:
> I want to check to see if a file exists that matches this pattern:
>
> OmniFocus-XXXXX.omnilicense
>
> where XXXXX is some set of numbers of an unknown length.
>
> but it occurs to me that I might be able to use [[ -e ]]
>
> However, I'm not sure what the syntax is. I tried:
>
> if [[ -e "OmniFocus.*.omnilicense" ]]
> then
>
> {take action here}
>
> fi
>
> but that did not work. Do I have the syntax wrong or do I need to make sure
> that some setting is enabled?

>From the man, section “conditional expressions” :

 “File generation is not  performed on any form of argument to conditions.”

Best regards,

-- 
Jérémie


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

* Re: checking for file existence when I don't know the exact name
  2011-10-24 13:16 checking for file existence when I don't know the exact name TJ Luoma
  2011-10-24 13:29 ` Jérémie Roquet
@ 2011-10-24 13:31 ` Peter Stephenson
  2011-10-24 13:41   ` Stephane CHAZELAS
  2011-10-24 14:36   ` Vincent Lefevre
  2011-10-24 13:35 ` Stephane CHAZELAS
  2 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2011-10-24 13:31 UTC (permalink / raw)
  To: Zsh Users

On Mon, 24 Oct 2011 09:16:21 -0400
TJ Luoma <luomat@gmail.com> wrote:
> I want to check to see if a file exists that matches this pattern:
> 
> OmniFocus-XXXXX.omnilicense
> 
> where XXXXX is some set of numbers of an unknown length.
> 
> I've been using
> 
> ls | egrep -q "OmniFocus.*\.omnilicense"
> 
> but it occurs to me that I might be able to use [[ -e ]]
> 
> However, I'm not sure what the syntax is. I tried:
> 
> if [[ -e "OmniFocus.*.omnilicense" ]]
> then
> 
> {take action here}
> 
> fi
> 
> but that did not work.

Tests don't do globbing.  They use pattern matching for other purposes
(if unquoted --- i.e. if it *did* work, which it doesn't, you wouldn't
want the double quotes).

The standard method is along the lines of:

matchingfiles() {
   emulate -L zsh
   local -a files
   files=(${~*}(N))
   (( ${#files} ))
}
if matchingfiles "OmniFocus.*.omnilicense"; then
  ...
fi

in which you now do need the double quotes.
-- 
Peter Stephenson <pws@csr.com>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


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

* Re: checking for file existence when I don't know the exact name
  2011-10-24 13:16 checking for file existence when I don't know the exact name TJ Luoma
  2011-10-24 13:29 ` Jérémie Roquet
  2011-10-24 13:31 ` Peter Stephenson
@ 2011-10-24 13:35 ` Stephane CHAZELAS
  2 siblings, 0 replies; 9+ messages in thread
From: Stephane CHAZELAS @ 2011-10-24 13:35 UTC (permalink / raw)
  To: zsh-users

2011-10-24, 09:16(-04), TJ Luoma:
> I want to check to see if a file exists that matches this pattern:
>
> OmniFocus-XXXXX.omnilicense
>
> where XXXXX is some set of numbers of an unknown length.
>
> I've been using
>
> ls | egrep -q "OmniFocus.*\.omnilicense"
>
> but it occurs to me that I might be able to use [[ -e ]]
>
> However, I'm not sure what the syntax is. I tried:
>
> if [[ -e "OmniFocus.*.omnilicense" ]]
> then
>
> {take action here}
[...]

matching_files=(OmniFocus.<->.omnilicense(N))
if (($#matching_files)); then...

-- 
Stephane


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

* Re: checking for file existence when I don't know the exact name
  2011-10-24 13:31 ` Peter Stephenson
@ 2011-10-24 13:41   ` Stephane CHAZELAS
  2011-10-24 14:36   ` Vincent Lefevre
  1 sibling, 0 replies; 9+ messages in thread
From: Stephane CHAZELAS @ 2011-10-24 13:41 UTC (permalink / raw)
  To: zsh-users

2011-10-24, 14:31(+01), Peter Stephenson:
[...]
> matchingfiles() {
>    emulate -L zsh
>    local -a files
>    files=(${~*}(N))

Shouldn't it be 
    files=(${^~*}(N))


>    (( ${#files} ))
> }
> if matchingfiles "OmniFocus.*.omnilicense"; then
>   ...
> fi
>
> in which you now do need the double quotes.

-- 
Stephane


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

* Re: checking for file existence when I don't know the exact name
  2011-10-24 13:29 ` Jérémie Roquet
@ 2011-10-24 14:25   ` TJ Luoma
  2011-10-24 14:59     ` Jérémie Roquet
  0 siblings, 1 reply; 9+ messages in thread
From: TJ Luoma @ 2011-10-24 14:25 UTC (permalink / raw)
  To: Jérémie Roquet; +Cc: Zsh Users

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

2011/10/24 Jérémie Roquet <arkanosis@gmail.com>

> From the man, section “conditional expressions” :
>
>  “File generation is not  performed on any form of argument to conditions.”
>

This isn't file _generation_, is it? Or am I not understanding zsh's terms?

TjL

ps -- thanks to the other responders as well. Glad to know I wasn't just
doing it wrong, but that it just doesn't work the way I thought. I guess I
still don't fully understand why / when to use [[ ]] instead of [ ]

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

* Re: checking for file existence when I don't know the exact name
  2011-10-24 13:31 ` Peter Stephenson
  2011-10-24 13:41   ` Stephane CHAZELAS
@ 2011-10-24 14:36   ` Vincent Lefevre
  1 sibling, 0 replies; 9+ messages in thread
From: Vincent Lefevre @ 2011-10-24 14:36 UTC (permalink / raw)
  To: zsh-users

On 2011-10-24 14:31:04 +0100, Peter Stephenson wrote:
> The standard method is along the lines of:
> 
> matchingfiles() {
>    emulate -L zsh
>    local -a files
>    files=(${~*}(N))
>    (( ${#files} ))
> }

Wouldn't this be more efficient with files=(${~*}(N[1]))?

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)


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

* Re: checking for file existence when I don't know the exact name
  2011-10-24 14:25   ` TJ Luoma
@ 2011-10-24 14:59     ` Jérémie Roquet
  2011-10-24 15:35       ` Peter Stephenson
  0 siblings, 1 reply; 9+ messages in thread
From: Jérémie Roquet @ 2011-10-24 14:59 UTC (permalink / raw)
  To: Zsh Users; +Cc: TJ Luoma

2011/10/24 TJ Luoma <luomat@gmail.com>:
> 2011/10/24 Jérémie Roquet <arkanosis@gmail.com>
>> From the man, section “conditional expressions” :
>>
>>  “File generation is not  performed on any form of argument to
>> conditions.”
>
> This isn't file _generation_, is it? Or am I not understanding zsh's terms?

Err, it's file*name* generation, actually. In zsh's terms, filename
generation refers to globbing.

Peter, maybe we should fix the doc there?

> I still don't fully understand why / when to use [[ ]] instead of [ ]

Most of the time, I guess. [[ ]] is part of the syntax and has a
number of advanced features while [ is just a command very similar to
“test” and ] an argument to this command.
Unless you want to keep your scripts portable accross different
shells, of course.

Best regards,

-- 
Jérémie

diff --git a/Doc/Zsh/cond.yo b/Doc/Zsh/cond.yo
index 489ee35..71971b4 100644
--- a/Doc/Zsh/cond.yo
+++ b/Doc/Zsh/cond.yo
@@ -184,7 +184,7 @@ enditem()
 Normal shell expansion is performed on the var(file), var(string) and
 var(pattern) arguments, but the result of each expansion is constrained to
 be a single word, similar to the effect of double quotes.
-File generation is not performed on any form of argument to conditions.
+Filename generation is not performed on any form of argument to conditions.
 However, pattern metacharacters are active for the var(pattern) arguments;
 the patterns are the same as those used for filename generation, see
 ifzman(\


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

* Re: checking for file existence when I don't know the exact name
  2011-10-24 14:59     ` Jérémie Roquet
@ 2011-10-24 15:35       ` Peter Stephenson
  0 siblings, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 2011-10-24 15:35 UTC (permalink / raw)
  To: Zsh Users

On Mon, 24 Oct 2011 16:59:43 +0200
Jérémie Roquet <arkanosis@gmail.com> wrote:
> 2011/10/24 TJ Luoma <luomat@gmail.com>:
> > 2011/10/24 Jérémie Roquet <arkanosis@gmail.com>
> >> From the man, section “conditional expressions” :
> >>
> >>  “File generation is not  performed on any form of argument to
> >> conditions.”
> >
> > This isn't file _generation_, is it? Or am I not understanding zsh's terms?
> 
> Err, it's file*name* generation, actually. In zsh's terms, filename
> generation refers to globbing.
> 
> Peter, maybe we should fix the doc there?

Yes, I've done that.

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


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


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

end of thread, other threads:[~2011-10-24 15:36 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-24 13:16 checking for file existence when I don't know the exact name TJ Luoma
2011-10-24 13:29 ` Jérémie Roquet
2011-10-24 14:25   ` TJ Luoma
2011-10-24 14:59     ` Jérémie Roquet
2011-10-24 15:35       ` Peter Stephenson
2011-10-24 13:31 ` Peter Stephenson
2011-10-24 13:41   ` Stephane CHAZELAS
2011-10-24 14:36   ` Vincent Lefevre
2011-10-24 13:35 ` Stephane CHAZELAS

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