ksh pattern matching

ksh pattern matching

Post by l.. » Sun, 19 Jan 1992 14:31:49

(pattern|pattern|...), seem to work only within [[ ]] and not
for pathname expansion, case pattern matching, or substring expansion.
And sometimes [[ ]] gets it wrong (I think.)  Is this
    * known misbehavior?
    * documented, and I didn't RTFM right?
    * a bug on my platforms only?  (11/16/88d, on RS6000 and RT)

Here's the script I used to test patterns with:
# called with a pattern as arg.

while read
echo ls: $(echo $REPLY)

for i in $(ls)
    if [[ $i = $REPLY ]] then print if: $i matches "$REPLY"
    else print if: $i does not match "$REPLY"

    case $i in

    case $i in
    !(alpha))   print case: $i matches literal '!(alpha)';;
    *)  print case: $i does not match literal '!(alpha)';;
    case $i in
    $REPLY)     print case: $i matches "$REPLY";;
    *)  print case: $i does not match "$REPLY";;
    echo $i'#'$REPLY' -> ' ${i#$REPLY}
    echo $i'##'$REPLY' -> ' ${i##$REPLY}
    echo $i'%'$REPLY' -> ' ${i%$REPLY}
    echo $i'%%'$REPLY' -> ' ${i%%$REPLY}


In a directory containing just two files 'alpha' and 'beta',
I execute the script above.  Here's input, output, and comments.
(input) *
ls: alpha beta
if: alpha matches *

case: alpha matches literal !(alpha)         // be reversed
case: alpha matches *
alpha#* ->  lpha                          // bug: shortest * match is empty
alpha##* ->
alpha%* ->  alpha                         // correct
alpha%%* ->
if: beta matches *

case: beta does not match literal !(alpha)   // conditions?
case: beta matches *
beta#* ->  eta                                    // same bug
beta##* ->
beta%* ->  beta
beta%%* ->

case: alpha matches literal !(alpha)

case: beta does not match literal !(alpha)

(input) *(beta)
if: alpha matches *(beta)                    // if is still wrong,

case: alpha matches literal !(alpha)
case: alpha does not match *(beta)           // but correct nonmatch in case!
alpha#*(beta) ->  alpha
alpha##*(beta) ->  alpha
alpha%*(beta) ->  alpha
alpha%%*(beta) ->  alpha
if: beta matches *(beta)                     // if seems to like *(beta)

case: beta does not match literal !(alpha)
case: beta does not match *(beta)            // and case doesn't.
beta#*(beta) ->  beta
beta##*(beta) ->  beta
beta%*(beta) ->  beta
beta%%*(beta) ->  beta

So what happens on your platform?



1. ksh pattern matching when pattern is in a variable

Witness this:

  [[ work = $exclude ]] && print excluded

The print is executed on AIX 433, but not on AIX 5.1 or SunOS 5.9.

Looking at the original AT&T ksh book by David Korn, on pg 156 under
Conditional Commands, in the [[ ... ]] section, it says

"ksh expands the operands(s) for each conditional expression primitive
for command substitution, parameter expansion, and quote removal as
required to evaluate the command."

To me, this means it should be expanding $exclude before it interprets
it as a pattern.  And this is certainaly The Right Thing To Do.  What
am I missing?


2. limit number of connects to port?

3. KSH Pattern Match Deletions

4. how can i user ftp cmd in shell program?

5. ksh pattern matching

6. qmail

7. Question: Ksh pattern matching operators

8. rewrite function as macro = interface change?

9. ksh pattern matching

10. Pattern matching and extracting the data which matches the pattern

11. ksh - checking for filenames matching a pattern

12. ksh conditional (string match pattern)

13. ksh discrepancy on pattern matching