From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18483 invoked by alias); 21 Feb 2012 13:52:25 -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: 16777 Received: (qmail 14708 invoked from network); 21 Feb 2012 13:52:22 -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, SPF_HELO_PASS autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at bewatermyfriend.org does not designate permitted sender hosts) From: Frank Terbeck To: zsh-users@zsh.org Subject: Re: somehow offtopic: looping filenames In-Reply-To: <20120221123536.GF23815@new.computing.dcu.ie> (Stephen Blott's message of "Tue, 21 Feb 2012 12:35:36 +0000") References: <20120220181650.GA11514@solfire> <120220110530.ZM450@torch.brasslantern.com> <20120221123536.GF23815@new.computing.dcu.ie> User-Agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.91 (gnu/linux) Date: Tue, 21 Feb 2012 14:35:39 +0100 Message-ID: <87k43g8g2c.fsf@ft.bewatermyfriend.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Df-Sender: [pbs]NDMwNDQ0 Stephen Blott wrote: [...] > ls | tr '\n' '\0' | xargs -0 -n 1 flac -- This is a pet peeve of mine, so I need to chime in... Using ls for generating file lists is always wrong. And always, there is a more robust way of dealing with the problem at hand using either shell globing or `find(1)'. (I used to have two "almost"s in that paragraph, but leaving them in would really overstate the relevance of possible situations=C2=B9.) There is nothing about this, that would improve the behaviour of a simple for loop like for i in *; flac -- $i (That's `short_loops' syntax, which is on by default in zsh. And it relies on `sh_word_split' being unset, which is also the default behaviour in zsh.) Worse, it's *less* robust, because it's treating newline characters specially, although they are perfectly legal characters in a file name (albeit not very common ones). Also, if the user has an alias in place, like say, alias ls=3D'ls -F' ...that is not going to work either, because that will cause ls to append a / to directory names, a * to executable files and so on. I know there are HOWTOs and tutorials online, that advocate constructs like that. And even worse ones, like "for i in `ls`; do...". But that doesn't make them good examples of shell programming. I don't mean to offend with this reply. And I hope that my point is making sense to you. I've written about this in the past=C2=B2 (but it's in German) and so has the guy who maintains the FAQ for #bash (the IRC channel for GNU bash on freenode)=C2=B3. Refer that that for more details on parsing the output from `ls'. Regards, Frank =C2=B9 There was one time, when someone asked me: Okay so "for i in `ls`" is bad, but what about when I want the generated list to be sorted by some criteria (like by modification time instead of the usual alphabetic order). And that's indeed not that easy with POSIX shell. In zsh, however, there are glob qualifiers like (O.) and (o.) (where`.' is a character that specifies a property to sort by), which allow the user to sort the result of a glob to his/her requirements. So that point is moot as well, if we're talking about zsh. =C2=B2 =C2=B3