Re^2: Idiom for $(ls -d $pattern 2>/dev/null) - easier method

Leo de Wit leo at philmds.UUCP
Sat Mar 25 21:13:58 AEST 1989


In article <2206 at solo2.cs.vu.nl> maart at cs.vu.nl (Maarten Litmaath) writes:
|leo at 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.



More information about the Comp.unix.questions mailing list