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

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



2015-01-19 16:44:27 -0500, Eric Cook:
[...]
> #!/bin/sh
> ${ZSH_VERSION+false} : || emulate sh
> match() {
>   test "$#" -gt 2 && return

Why not "-gt 1"?

>   test -e "$1"    && return

test -e

will return false for a symlink to an inaccessible file. So
you'll want a test -L "$1" as well.

>   return 1
> }
> 
> set --
> for pat in '..?*' '.[!.]*' '*'; do # I moved your added pattern to where
> POSIX locale would sort them.
>   if match $pat; then
>     set -- "$@" $pat

Note that for that to work in the Bourne shell, IFS needs to
contain space (and none of the characters in the patterns in all
Bourne-like shells).

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

-- 
Stephane



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