zsh-users
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Nikolai Weibull <now@disu.se>
Cc: Eric Cook <llua@gmx.com>, "zsh-users@zsh.org" <zsh-users@zsh.org>
Subject: Re: Equivalent of set -- *(DN) in sh
Date: Thu, 22 Jan 2015 15:21:40 +0000	[thread overview]
Message-ID: <20150122152139.GB8163@chaz.gmail.com> (raw)
In-Reply-To: <CADdV=MsvuSAQMJVsr17Y7g2Nfjy95CQ007opQqv-7=RHjgjaKw__35342.9068615243$1421913204$gmane$org@mail.gmail.com>

2015-01-22 08:44:40 +0100, Nikolai Weibull:
[...]
> set x *; shift
> test $# -eq 1 && test "x$1" = x\* && ! test -e \* && shift
> n=$#
> set x .[!.]* ${1+"$@"}; shift
> test $# -eq `expr $n + 1` && test "x$1" = 'x.[!.]?*' && ! test -e
> '.[!.]?*' && shift
> n=$#
> set x ..?* ${1+"$@"}; shift
> test $# -eq `expr $n + 1` && test ="x$1" = 'x..?*' && ! test -e '..?*' && shift
> 
> This tries to avoid using test -e (or whatever version of testing for
> a files existence you’d like to use)

Well, it doesn't try very hard does it ;-) ? Remember you need
test -e || test -L (or test -h on some old systems).

> but also avoids using patterns
> just to test for failures to avoid unnecessary directory traversals.

The directory content will be in cache after the first `set x
*`. Forking and exeuting some exprs is probably going to be
worse in most cases.

> It also avoids messing with IFS, but perhaps that’s necessary?

Well, yes "$@" in the Bourne shell relies on it containing space
and you're leaving a number of variables and command
substitutions unquoted.

Note that "case" has far fewer problems than "test".

> 
> The ${1+"$@"} is to avoid an old Zsh bug, right?

No, that's the other way round. ${1+"$@"} is to work around
problems with the Bourne shell (at least those where that has
not been fixed (where "$@" expands to one empty argument when $#
is 0)). zsh is the one that had problems with
${1+"$@"} (not "$@"), not really a bug, but down to how
parameter expansion nested.

Which is why you see things like:

test -z "$ZSH_VERSION" || alias -g '${1+"$@"}="$@"'

> It also avoids set --, as that’s apparently not portable either.


According to http://www.in-ulm.de/~mascheck/bourne/, -- was
added to the Bourne shell at the same time as [!...], so that
would suggest that if you want to account for those very old
systems, you can't use [!...] either. Note that old Bourne
shells didn't support characters with the 8th bit set either.

-- 
Stephane


      parent reply	other threads:[~2015-01-22 15:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-18 18:28 Nikolai Weibull
2015-01-18 19:07 ` Roman Neuhauser
2015-01-18 19:43   ` Nikolai Weibull
2015-01-18 20:32     ` Roman Neuhauser
2015-01-18 19:31 ` ZyX
2015-01-18 19:46   ` Nikolai Weibull
2015-01-18 20:46 ` Eric Cook
2015-01-19 15:51   ` ZyX
2015-01-19 15:55     ` ZyX
2015-01-19 16:02       ` ZyX
2015-01-19 16:16         ` ZyX
2015-01-19 21:44     ` Eric Cook
     [not found]     ` <54BD7ABB.5070501__36205.2317861982$1421704010$gmane$org@gmx.com>
2015-01-19 23:05       ` Stephane Chazelas
2015-01-22  7:44         ` Nikolai Weibull
     [not found]         ` <CADdV=MsvuSAQMJVsr17Y7g2Nfjy95CQ007opQqv-7=RHjgjaKw__35342.9068615243$1421913204$gmane$org@mail.gmail.com>
2015-01-22 15:21           ` Stephane Chazelas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150122152139.GB8163@chaz.gmail.com \
    --to=stephane.chazelas@gmail.com \
    --cc=llua@gmx.com \
    --cc=now@disu.se \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).