Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!lll-winken!uunet!mcvax!hp4nl!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.unix.questions Subject: Re: Re^2: Idiom for $(ls -d $pattern 2>/dev/null) - easier method Message-ID: <991@philmds.UUCP> Date: 25 Mar 89 11:13:58 GMT References: <21939@shemp.CS.UCLA.EDU> <2189@solo11.cs.vu.nl> <988@philmds.UUCP> <2206@solo2.cs.vu.nl> Reply-To: leo@philmds.UUCP (Leo de Wit) Organization: Philips I&E DTS Eindhoven Lines: 39 In article <2206@solo2.cs.vu.nl> maart@cs.vu.nl (Maarten Litmaath) writes: |leo@philmds.UUCP (Leo de Wit) writes: [my shell script] |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" But this fails to recognize patterns without meta-characters. For instance: if you have a file named foo, and pattern=foo. My method _does_ recognize that, and correctly decides whether the file exists. |Shortcomings: Leo's script will incorrectly let "*[QZ]" match itself, That is correct; I overlooked the fact that one can construct a filename that will not match itself as a wildcard expression. It has to be pretty weird, though (all wildcard expressions consisting of normal characters, * and ? always match themselves as filenames, if I'm not mistaken, so it has to contain a [...] range expression. Even this can probably be handled if one tries hard enough). |my script won't let * match itself. |I guess one needs at least 1 invocation of `test'. If test is not a builtin, I'd probably go for the ls -d $pattern 2>/dev/null method; it is shorter, clearer and probably just as fast. Leo.