Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Equivalent of set -- *(DN) in sh



On Tue, Jan 20, 2015 at 12:05 AM, Stephane Chazelas
<stephane.chazelas@xxxxxxxxx> wrote:

> Another approach that only relies on reading the directory
> contents (not "stat"ing the files):
>
> set -- [*] *
> case $1$2 in
>   '[*]*') shift 2;;
>   *) shift
> esac
> IFS=" "
> set -- .[.]?* ${1+"$@"}
> case $1 in '.[.]?*') shift; esac
> set -- .[[]'!.]?*' .[!.]?* ${1+"$@"}
> case $1$2 in
>   '.[[]!.]?*.[!.]?*') shift 2;;
>   *) shift
> esac
>
> (won't give the same order as *(ND) either).

How about

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), but also avoids using patterns
just to test for failures to avoid unnecessary directory traversals.

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

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

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



Messages sorted by: Reverse Date, Date, Thread, Author