Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!hp4nl!botter!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.unix.questions Subject: Re^2: Idiom for $(ls -d $pattern 2>/dev/null) - easier method Message-ID: <2206@solo2.cs.vu.nl> Date: 23 Mar 89 08:43:13 GMT References: <21939@shemp.CS.UCLA.EDU> <2189@solo11.cs.vu.nl> <988@philmds.UUCP> Organization: V.U. Informatica, Amsterdam, the Netherlands Lines: 57 leo@philmds.UUCP (Leo de Wit) writes: \set - $pattern # expand into $* \case $# in \1) p1="$1" # one arg only; could be mismatch. \ set - $pattern?* # retry with other pattern (one char extra). \ n2=$# # save number of matches. \ set - $pattern* # retry with yet another pattern. \ case $# in # 1 <= $n2 <= $# holds now. \ 1) case $1 in \ "$p1") : ;; # valid match on $p1. ^^^^^^^^^^^ not always (see below) \ *) shift;; # no match; empty $*. \ esac;; \ $n2) set ""; shift;; # matches for $pattern* == # matches for $pattern?*, \ # so no match on $p1; empty $*. \ *) set "$p1";; # $# > n2 so there was a valid match on $p1. \ esac;; \esac That can be done far more easier! set - $pattern case $# in 1) case $pattern in $1) shift # neglect pathological file names like *[QZ] esac esac Notice the trick: normally $pattern won't match $1 - "bin" matches "b??", but "b??" doesn't match "bin" Shortcomings: Leo's script will incorrectly let "*[QZ]" match itself, my script won't let * match itself. I guess one needs at least 1 invocation of `test'. The `shift' statement should be replaced with: case $1 in *[[?*]*) [ -f $1 -o -d $1 ] || shift ;; *) shift esac If `patterns' without special characters are allowed, simply: [ -f $1 -o -d $1 ] || shift -- Modeless editors and strong typing: |Maarten Litmaath @ VU Amsterdam: both for people with weak memories. |maart@cs.vu.nl, mcvax!botter!maart