zsh-users
 help / color / mirror / code / Atom feed
From: ZyX <kp-pav@yandex.ru>
To: Eric Cook <llua@gmx.com>, "zsh-users@zsh.org" <zsh-users@zsh.org>
Subject: Re: Equivalent of set -- *(DN) in sh
Date: Mon, 19 Jan 2015 19:16:49 +0300	[thread overview]
Message-ID: <1178831421684209@web6h.yandex.ru> (raw)
In-Reply-To: <1136081421683325@web6h.yandex.ru>



19.01.2015, 19:02, "ZyX" <kp-pav@yandex.ru>:
> 19.01.2015, 18:55, "ZyX" <kp-pav@yandex.ru>:
>>  19.01.2015, 18:51, "ZyX" <kp-pav@yandex.ru>:
>>>   18.01.2015, 23:53, "Eric Cook" <llua@gmx.com>:
>>>>    On 01/18/2015 01:28 PM, Nikolai Weibull wrote:
>>>>>     Hi!
>>>>>
>>>>>     Is there any way to get the equivalent of Zsh’s
>>>>>
>>>>>     set -- *(DN)
>>>>>
>>>>>     in sh?  Most important here would be NULL_GLOB, as, by default, sh
>>>>>     simply leaves the * if there are no files to match.
>>>>>
>>>>>     Thanks!
>>>>    match() {
>>>>      test "$#" -gt 2 && return
>>>>      test -e "$1"    && return
>>>>      return 1
>>>>    }
>>>>
>>>>    set --
>>>>    for pat in '.[^.]*' '*'; do # *(DN) ignores . and ..
>>>   `..foo` is a valid name, but it is being excluded. You need to add `'.??*'` to the list of patterns.
>>  No, this may make duplicates. Then `'..?*'`.
>
> And you must replace `[^.]` with `[!.]`. mksh does not support `[^]` and treats this as `[\^.]`, but other shells I have (dash, ksh, zsh (in sh emulation mode), bash, busybox ash) are fine with both `[!.]` and `[^.]`.
>>>>      if match $pat; then
>>>>        set -- "$@" $pat
>>>>      fi
>>>>    done
>>>>    unset pat
>>>>
>>>>    test "$#" -gt 0 && printf '%s\n' "$@"

Also note that if you need *full* equivalent of *(DN) you need to do something with ordering. I have checked all the shells with

    for sh in dash ksh mksh zsh bash bb ; do $sh set--sDN.sh > set--sDN.$sh.log ; done

with

    emulate -L sh &>/dev/null
    match() {
      test "$#" -gt 2 && return
      test -e "$1"    && return
      return 1
    }
    
    set --
    for pat in '.[!.]*' '..?*' '*'; do # *(DN) ignores . and ..
      if match $pat; then
        set -- "$@" $pat
      fi
    done
    unset pat
    
    test "$#" -gt 0 && printf '>>%s<<\n' "$@"

in set--sDN.sh file and have the following md5sums:

    % md5sum set--sDN.*.log | sort
    4601e6d9c845fe88e5166db3be04cc75  set--sDN.bash.log
    4601e6d9c845fe88e5166db3be04cc75  set--sDN.ksh.log
    4601e6d9c845fe88e5166db3be04cc75  set--sDN.zsh.log
    ac2c245cdac664121a348c6b05ea2f44  set--sDN.bb.log
    ac2c245cdac664121a348c6b05ea2f44  set--sDN.dash.log
    ac2c245cdac664121a348c6b05ea2f44  set--sDN.mksh.log

: two variants of ordering:

bash/ksh/zsh:

    >>.abc<<
    >>.b<<
    >>.^foo<<
    >>..foo<<
    >>
    <<
    >>1<<
    >>17<<
    >>2<<
    >>append-remove.bash<<
    >>getnumbers.bash<<
    >>grepdir<<
    >>nroff<<
    >>parinput.bash<<
    >>qwerty<<
    >>rl.sh<<
    >>script_path.bash<<
    >>set--sDN.bash.log<<
    >>set--sDN.bb.log<<
    >>set--sDN.dash.log<<
    >>set--sDN.ksh.log<<
    >>set--sDN.mksh.log<<
    >>set--sDN.sh<<
    >>set--sDN.zsh.log<<
    >>tes.bash<<
    >>test<<
    >>test2.bash<<
    >>test3.bash<<
    >>test.bash<<
    >>testdir<<
    >>test-rename.sh<<
    >>test-so-2914220.bash<<
    >>t.tar<<
    >>ttttt.tar.bz2<<
    >>ttttt.tar.gz<<

bb/dash/mksh:

    >>.^foo<<
    >>.abc<<
    >>.b<<
    >>..foo<<
    >>
    <<
    >>1<<
    >>17<<
    >>2<<
    >>append-remove.bash<<
    >>getnumbers.bash<<
    >>grepdir<<
    >>nroff<<
    >>parinput.bash<<
    >>qwerty<<
    >>rl.sh<<
    >>script_path.bash<<
    >>set--sDN.bash.log<<
    >>set--sDN.bb.log<<
    >>set--sDN.dash.log<<
    >>set--sDN.ksh.log<<
    >>set--sDN.mksh.log<<
    >>set--sDN.sh<<
    >>set--sDN.zsh.log<<
    >>t.tar<<
    >>tes.bash<<
    >>test<<
    >>test-rename.sh<<
    >>test-so-2914220.bash<<
    >>test.bash<<
    >>test2.bash<<
    >>test3.bash<<
    >>testdir<<
    >>ttttt.tar.bz2<<
    >>ttttt.tar.gz<<

(note position of t.tar).

I have verified that all tests emit essentially the same result by using `for f in set--sDN.*.log ; do cat $f | sort | md5sum ; done | uniq`. But both orderings are different from the zsh one:

    % zsh -f -c 'for f in *(DN); do echo ">>$f<<" ; done'
    >>
    <<
    >>1<<
    >>17<<
    >>2<<
    >>.abc<<
    >>append-remove.bash<<
    >>.b<<
    >>.^foo<<
    >>..foo<<
    >>getnumbers.bash<<
    >>grepdir<<
    >>nroff<<
    >>parinput.bash<<
    >>qwerty<<
    >>rl.sh<<
    >>script_path.bash<<
    >>set--sDN.bash.log<<
    >>set--sDN.bb.log<<
    >>set--sDN.dash.log<<
    >>set--sDN.ksh.log<<
    >>set--sDN.mksh.log<<
    >>set--sDN.sh<<
    >>set--sDN.zsh.log<<
    >>tes.bash<<
    >>test<<
    >>test2.bash<<
    >>test3.bash<<
    >>test.bash<<
    >>testdir<<
    >>test-rename.sh<<
    >>test-so-2914220.bash<<
    >>t.tar<<
    >>ttttt.tar.bz2<<
    >>ttttt.tar.gz<<

(note: I have filename with newline, this is why I added `>><<`.)


  reply	other threads:[~2015-01-19 16:27 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 [this message]
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

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=1178831421684209@web6h.yandex.ru \
    --to=kp-pav@yandex.ru \
    --cc=llua@gmx.com \
    --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).