From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3843 invoked by alias); 4 Mar 2012 18:38:26 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 16829 Received: (qmail 235 invoked from network); 4 Mar 2012 18:38:14 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <120304103757.ZM24588@torch.brasslantern.com> Date: Sun, 04 Mar 2012 10:37:57 -0800 In-reply-to: <20120304143102.GE18164@solfire> Comments: In reply to meino.cramer@gmx.de "if the file is not found the files is not found is the file not found" (Mar 4, 3:31pm) References: <20120304143102.GE18164@solfire> In-reply-to: Comments: In reply to Mikael Magnusson "Re: if the file is not found the files is not found is the file not found" (Mar 4, 3:37pm) X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: if the file is not found the files is not found is the file not found MIME-version: 1.0 Content-type: text/plain; charset=us-ascii 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