zsh-users
 help / color / mirror / code / Atom feed
* if the file is not found the files is not found is the file not found
@ 2012-03-04 14:31 meino.cramer
  2012-03-04 14:36 ` Moritz Bunkus
  2012-03-04 14:37 ` Mikael Magnusson
  0 siblings, 2 replies; 9+ messages in thread
From: meino.cramer @ 2012-03-04 14:31 UTC (permalink / raw)
  To: zsh-users

Hi,

 ... the cat has bitten into its own tail... somehow...


I wrote a script which handles dvbt streamed files. The process
creates some temporary files, which I want to remove afterwards,
because they normally big ones. 

First I wrote

    rm -f ${f}-[0-9]*.mp2 

which breaks which an error, if the certain has not created files
of that pattern ... despite the "-f" of the "rm" command.

Then I treid to check for the existence of such files in beforehand
this way:

     [ -f ${f}-[0-9]*.mp2 ]] && rm -f ${f}-[0-9]*.mp2

. Which fails for the same reason.

Did I get lost here? ;)

How many cats do I need to get one, which does not bit into
the tail of the next cat I enter into the script ?

Who knows of the according dog to chase the cats away and make
my script work? ;) :))

Thanks a lot for any help in advance!

Best regards,
mcc



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

* Re: if the file is not found the files is not found is the file not found
  2012-03-04 14:31 if the file is not found the files is not found is the file not found meino.cramer
@ 2012-03-04 14:36 ` Moritz Bunkus
  2012-03-04 14:37 ` Mikael Magnusson
  1 sibling, 0 replies; 9+ messages in thread
From: Moritz Bunkus @ 2012-03-04 14:36 UTC (permalink / raw)
  To: zsh-users

Hey,

On Sun, Mar 4, 2012 at 15:31,  <meino.cramer@gmx.de> wrote:

> First I wrote
>
>    rm -f ${f}-[0-9]*.mp2
>
> which breaks which an error, if the certain has not created files
> of that pattern ... despite the "-f" of the "rm" command.

The error probably comes from zsh that it cannot match any file. Try
again with "setopt nullglob"; in that case the pattern not matching
anything will simply be replaced by nothing and the command executed.

Kind regards,
mo


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

* Re: if the file is not found the files is not found is the file not found
  2012-03-04 14:31 if the file is not found the files is not found is the file not found meino.cramer
  2012-03-04 14:36 ` Moritz Bunkus
@ 2012-03-04 14:37 ` Mikael Magnusson
  2012-03-04 18:37   ` Bart Schaefer
  1 sibling, 1 reply; 9+ messages in thread
From: Mikael Magnusson @ 2012-03-04 14:37 UTC (permalink / raw)
  To: meino.cramer; +Cc: zsh-users

On 4 March 2012 15:31,  <meino.cramer@gmx.de> wrote:
> Hi,
>
>  ... the cat has bitten into its own tail... somehow...
>
>
> I wrote a script which handles dvbt streamed files. The process
> creates some temporary files, which I want to remove afterwards,
> because they normally big ones.
>
> First I wrote
>
>    rm -f ${f}-[0-9]*.mp2
>
> which breaks which an error, if the certain has not created files
> of that pattern ... despite the "-f" of the "rm" command.
>
> Then I treid to check for the existence of such files in beforehand
> this way:
>
>     [ -f ${f}-[0-9]*.mp2 ]] && rm -f ${f}-[0-9]*.mp2
>
> . Which fails for the same reason.
>
> Did I get lost here? ;)
>
> How many cats do I need to get one, which does not bit into
> the tail of the next cat I enter into the script ?
>
> Who knows of the according dog to chase the cats away and make
> my script work? ;) :))
>
> Thanks a lot for any help in advance!

If you setopt extendedglob you can append (#qN) to the pattern to
suppress the error for that specific pattern, or setopt nullglob to
always do it. ie, it would be ${f}-[0-9]*.mp2(#qN)

-- 
Mikael Magnusson


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

* Re: if the file is not found the files is not found is the file not found
  2012-03-04 14:37 ` Mikael Magnusson
@ 2012-03-04 18:37   ` Bart Schaefer
  2012-03-04 18:48     ` Mikael Magnusson
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2012-03-04 18:37 UTC (permalink / raw)
  To: zsh-users

On Mar 4,  3:31pm, meino.cramer@gmx.de wrote:
}
}      [ -f ${f}-[0-9]*.mp2 ]] && rm -f ${f}-[0-9]*.mp2

This won't work even if the files do exist, because you can't apply
a single "-f" test to the multiple files that result from the glob.

Also you've used "[" on the left but "]]" on the right, which is
mostly nonsense.  If you instead used [[ on the left then the glob
would not be expanded and the test would again fail.

On Mar 4,  3:37pm, Mikael Magnusson wrote:
}
} If you setopt extendedglob you can append (#qN) to the pattern

You don't even need extendedglob -- you just need bare_glob_qual, which
is on by default unless you're in sh/ksh emulation modes:

    rm -f ${f}-[0-9]*.mp2(N)

I'm sure the archives of zsh-users hold many different answers to the
question, "Given a file pattern, how do I test whether at least one
matching file exists?"  Unfortunately, there's really no way to do so
in a single operation unless you set the no_nomatch option.  [[ ]]
does not perform globbing, and the test operators such as [ -f ] are
defined to return TRUE rather than FALSE on a *missing* file name
operand, so null_glob is not sufficient.

And yet we've never added a glob qualifier to invert nomatch (nor to
invert bad_pattern) for a single glob ... however, we did invent
anonymous functions, so you can make multiple operations look like a
single operation:

if (){ setopt localoptions no_nomatch; [ -f ${f}-[0-9]*.mp2([1]) ]; }
then rm -f ${f}-[0-9]*.mp2
fi

The "if (){ ... }" does not define a function named "if" because "if"
is a reserved word.  The ([1]) qualifier on the end of the pattern
extracts only the first matching file so that you aren't passing too
many arguments to "[ -f ... ]".

If you don't like that syntax and don't mind an extra process, you can
also do this with a subshell:

if ( setopt no_nomatch; [ -f ${f}-[0-9]*.mp2([1]) ]; )
then rm -f ${f}-[0-9]*.mp2
fi

Woof woof.

-- 
Barton E. Schaefer


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

* Re: if the file is not found the files is not found is the file not found
  2012-03-04 18:37   ` Bart Schaefer
@ 2012-03-04 18:48     ` Mikael Magnusson
  2012-03-04 19:44       ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Mikael Magnusson @ 2012-03-04 18:48 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

On 4 March 2012 19:37, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Mar 4,  3:31pm, meino.cramer@gmx.de wrote:
> }
> }      [ -f ${f}-[0-9]*.mp2 ]] && rm -f ${f}-[0-9]*.mp2
>
> This won't work even if the files do exist, because you can't apply
> a single "-f" test to the multiple files that result from the glob.
>
> Also you've used "[" on the left but "]]" on the right, which is
> mostly nonsense.  If you instead used [[ on the left then the glob
> would not be expanded and the test would again fail.
>
> On Mar 4,  3:37pm, Mikael Magnusson wrote:
> }
> } If you setopt extendedglob you can append (#qN) to the pattern
>
> You don't even need extendedglob -- you just need bare_glob_qual, which
> is on by default unless you're in sh/ksh emulation modes:
>
>    rm -f ${f}-[0-9]*.mp2(N)

Ah, I've had them both on since forever and knew extendedglob wasn't
on by default, so I assumed bareglobquals was also off.

> I'm sure the archives of zsh-users hold many different answers to the
> question, "Given a file pattern, how do I test whether at least one
> matching file exists?"  Unfortunately, there's really no way to do so
> in a single operation unless you set the no_nomatch option.  [[ ]]
> does not perform globbing, and the test operators such as [ -f ] are
> defined to return TRUE rather than FALSE on a *missing* file name
> operand, so null_glob is not sufficient.

Here's one more for the collection,
if () { (( $# )) } arglblargh*(N[1]); then echo yes; else echo no; fi

But this needs the 'new' anon functions with arguments support.

-- 
Mikael Magnusson


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

* Re: if the file is not found the files is not found is the file not found
  2012-03-04 18:48     ` Mikael Magnusson
@ 2012-03-04 19:44       ` Bart Schaefer
  2012-03-04 20:13         ` Mikael Magnusson
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2012-03-04 19:44 UTC (permalink / raw)
  To: zsh-users

On Mar 4,  7:48pm, Mikael Magnusson wrote:
>
> On 4 March 2012 19:37, Bart Schaefer <schaefer@brasslantern.com> wrote:
> > I'm sure the archives of zsh-users hold many different answers to the
> > question, "Given a file pattern, how do I test whether at least one
> > matching file exists?"
> 
> Here's one more for the collection,
> if () { (( $# )) } arglblargh*(N[1]); then echo yes; else echo no; fi

Yes, I was thinking about that but it doesn't capture the "is a plain
file" semantics of [[ -f ]] -- which you can fix by adding qualifiers
to the glob instead, of course -- and even my formulation falls down
if the glob matches a mix of plain and not-plain files and the first
one happens to be the wrong kind.

(Also, the [1] in your formula is extraneous, but that's a nit.)

Really what one means with [ -f foo* ] is usually one of

# One existing file matching foo* is a plain file
(){ local f; for f; do [ -f "$f" ] && return 0; done; return 1; } foo*(N)

or

# All files matching foo* exist and are plain files
(){ local f; for f; do [ -f "$f" ] || return 1; done; (( $# )); } foo*(N)

Still another way to do "one foo* is plain":

(){ (( $# )) } foo*(Ne:'[ -f "$REPLY" ]':)

I'm not sure there's a way to use (e::) for "every foo* is plain".

-- 
Barton E. Schaefer


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

* Re: if the file is not found the files is not found is the file not found
  2012-03-04 19:44       ` Bart Schaefer
@ 2012-03-04 20:13         ` Mikael Magnusson
  2012-03-04 21:47           ` Bart Schaefer
  0 siblings, 1 reply; 9+ messages in thread
From: Mikael Magnusson @ 2012-03-04 20:13 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

On 4 March 2012 20:44, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Mar 4,  7:48pm, Mikael Magnusson wrote:
>>
>> On 4 March 2012 19:37, Bart Schaefer <schaefer@brasslantern.com> wrote:
>> > I'm sure the archives of zsh-users hold many different answers to the
>> > question, "Given a file pattern, how do I test whether at least one
>> > matching file exists?"
>>
>> Here's one more for the collection,
>> if () { (( $# )) } arglblargh*(N[1]); then echo yes; else echo no; fi
>
> Yes, I was thinking about that but it doesn't capture the "is a plain
> file" semantics of [[ -f ]] -- which you can fix by adding qualifiers
> to the glob instead, of course -- and even my formulation falls down
> if the glob matches a mix of plain and not-plain files and the first
> one happens to be the wrong kind.
>
> (Also, the [1] in your formula is extraneous, but that's a nit.)

I like to imagine that it causes less data to be copied around. :)

-- 
Mikael Magnusson


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

* Re: if the file is not found the files is not found is the file not found
  2012-03-04 20:13         ` Mikael Magnusson
@ 2012-03-04 21:47           ` Bart Schaefer
  2012-03-05  0:48             ` Mikael Magnusson
  0 siblings, 1 reply; 9+ messages in thread
From: Bart Schaefer @ 2012-03-04 21:47 UTC (permalink / raw)
  To: zsh-users

On Mar 4,  9:13pm, Mikael Magnusson wrote:
}
} >> if () { (( $# )) } arglblargh*(N[1]); then echo yes; else echo no; fi
} >
} > (Also, the [1] in your formula is extraneous, but that's a nit.)
} 
} I like to imagine that it causes less data to be copied around. :)

According to valgrind, for a pattern matching six files the ([1]) saves
3 mallocs and 3 frees but uses 346 *more* bytes of memory.  "time" on
10 runs of each shows no difference at all.

The ([1]) does begin to win on memory as well when the number of matched
files grows, but even at 360+ files no difference in time is detectable,
probably because the vast bulk is taken up by doing the glob in the first
place.

(e:'[ -f "$REPLY" ]':) takes about 3 times as long and uses 5 times as
much memory, with or without ([1]) added.  (e:'[[ -f "$REPLY" ]]') is
better, only double time and triple memory, and is on a par with adding
(.) as a qualifier.

So there you go. :-)


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

* Re: if the file is not found the files is not found is the file not found
  2012-03-04 21:47           ` Bart Schaefer
@ 2012-03-05  0:48             ` Mikael Magnusson
  0 siblings, 0 replies; 9+ messages in thread
From: Mikael Magnusson @ 2012-03-05  0:48 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

On 4 March 2012 22:47, Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Mar 4,  9:13pm, Mikael Magnusson wrote:
> }
> } >> if () { (( $# )) } arglblargh*(N[1]); then echo yes; else echo no; fi
> } >
> } > (Also, the [1] in your formula is extraneous, but that's a nit.)
> }
> } I like to imagine that it causes less data to be copied around. :)
>
> According to valgrind, for a pattern matching six files the ([1]) saves
> 3 mallocs and 3 frees but uses 346 *more* bytes of memory.  "time" on
> 10 runs of each shows no difference at all.
>
> The ([1]) does begin to win on memory as well when the number of matched
> files grows, but even at 360+ files no difference in time is detectable,
> probably because the vast bulk is taken up by doing the glob in the first
> place.
>
> (e:'[ -f "$REPLY" ]':) takes about 3 times as long and uses 5 times as
> much memory, with or without ([1]) added.  (e:'[[ -f "$REPLY" ]]') is
> better, only double time and triple memory, and is on a par with adding
> (.) as a qualifier.
>
> So there you go. :-)

Heh, thanks for checking :). Btw,
% () { echo $# } *
2411
zsh -c 'repeat 1000 () { (($#)) } *'  6.53s user 0.40s system 98% cpu
6.999 total
zsh -c 'repeat 1000 () { (($#)) } *([1])'  5.97s user 0.38s system 99%
cpu 6.399 total
(numbers are consistent within 0.02s)

So maybe it doesn't save the time it takes to type the [1]. Adding oN
on the other hand,
zsh -c 'repeat 1000 () { (($#)) } *(oN)'  4.81s user 0.40s system 99%
cpu 5.252 total
zsh -c 'repeat 1000 () { (($#)) } *(oN[1])'  4.31s user 0.36s system
99% cpu 4.703 total

For those that don't know, (oN) disables sorting the results of the glob.

-- 
Mikael Magnusson


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

end of thread, other threads:[~2012-03-05  0:48 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-04 14:31 if the file is not found the files is not found is the file not found meino.cramer
2012-03-04 14:36 ` Moritz Bunkus
2012-03-04 14:37 ` Mikael Magnusson
2012-03-04 18:37   ` Bart Schaefer
2012-03-04 18:48     ` Mikael Magnusson
2012-03-04 19:44       ` Bart Schaefer
2012-03-04 20:13         ` Mikael Magnusson
2012-03-04 21:47           ` Bart Schaefer
2012-03-05  0:48             ` Mikael Magnusson

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