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

Maarten Litmaath maart at cs.vu.nl
Thu Mar 23 18:43:13 AEST 1989


leo at 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 at cs.vu.nl, mcvax!botter!maart



More information about the Comp.unix.questions mailing list