ksh discrepancy on pattern matching

ksh discrepancy on pattern matching

Post by Clayton E. Cram » Wed, 05 Mar 2003 07:09:35



I am starting to port a vast swarm of ksh scripts from HP-UX to RedHat
Linux 7.2.  I have immediately run into a discrepancy in ksh that has
me rather worried.

Here's a sample script:

#!/bin/ksh
Citrus="orange|lemon|lime|grapefruit"

then
        echo "not a citrus fruit"
else
        echo "$1 is a citrus fruit"
fi

This works fine under HP-UX; ~/temp2 orange print "orange is a citrus
fruit".
Under Linux, it fails to match the pattern--apparently, the | isn't
being recognized as a string separator.  If I change the if statement
to:


it works just fine on both HP-UX and Linux.

Now, when I read the manual, none of the examples are of the form:

Citrus="orange|lemon|lime|grapefruit"

but of the form:


I tried escaping the | with \, using ' instead of " around the list of
strings, to no avail.  Any suggestions?

I expect that I am going to be a regular reader of this newsgroup for
the next few months, but I would appreciate if you could email me as
well as post your suggestions or answers.

 
 
 

ksh discrepancy on pattern matching

Post by Heiner Steve » Wed, 05 Mar 2003 08:38:14


 > I am starting to port a vast swarm of ksh scripts from HP-UX to RedHat
 > Linux 7.2.  I have immediately run into a discrepancy in ksh that has
 > me rather worried.
 >
 > Here's a sample script:
 >
 > #!/bin/ksh
 > Citrus="orange|lemon|lime|grapefruit"

 > then
 >   echo "not a citrus fruit"
 > else
 >   echo "$1 is a citrus fruit"
 > fi
 >
 > This works fine under HP-UX; ~/temp2 orange print "orange is a citrus
 > fruit".
 > Under Linux, it fails to match the pattern--apparently, the | isn't
 > being recognized as a string separator.

ksh93 shows the same behaviour, so it's probably a feature,
not a bug ;-/

If you change it to

    #!/bin/ksh
    Citrus="orange|lemon|lime|grapefruit"

    then
        echo "not a citrus fruit"
    else
        echo "$1 is a citrus fruit"
    fi

it works with Linux pdksh and Linux ksh93 (I cannot check
HP-UX).

Heiner
--
 ___ _

\__ \  _/ -_) V / -_) ' \    Shell Script Programmers: visit
|___/\__\___|\_/\___|_||_|   http://www.shelldorado.com/

 
 
 

ksh discrepancy on pattern matching

Post by Michael Wa » Wed, 05 Mar 2003 08:44:19




>#!/bin/ksh
>Citrus="orange|lemon|lime|grapefruit"

>then
>    echo "not a citrus fruit"
>else
>    echo "$1 is a citrus fruit"
>fi

I believe there was a bug in ksh88, and get fixed in ksh93.

There are two solutions:

a) Always use ksh93 from http://www.kornshell.com/.
b) add "eval" between "if" and "[[".

Posted and emailed.

--

 
 
 

ksh discrepancy on pattern matching

Post by Stephane CHAZELA » Wed, 05 Mar 2003 17:27:50


[...]
> If you change it to

>     #!/bin/ksh
>     Citrus="orange|lemon|lime|grapefruit"

>     then
>         echo "not a citrus fruit"
>     else
>         echo "$1 is a citrus fruit"
>     fi

> it works with Linux pdksh and Linux ksh93 (I cannot check
> HP-UX).

[...]

And if you change it to:

Citrus="orange|lemon|lime|grapefruit"
case "|$Citrus|" in
 *"|$1|"*)
   echo "$1 is a citrus fruit";;
 *)
   echo "not a citrus fruit";;
esac

It will work with any Bourne like shell.

--
Stphane

 
 
 

ksh discrepancy on pattern matching

Post by Bill Marcu » Thu, 06 Mar 2003 01:13:53


On 3 Mar 2003 14:09:35 -0800, Clayton E. Cramer

> I am starting to port a vast swarm of ksh scripts from HP-UX to RedHat
> Linux 7.2.  I have immediately run into a discrepancy in ksh that has
> me rather worried.

The ksh that comes with Red Hat Linux is probably pdksh.  AFAIK it's
not a bad shell, but it is different from "real" ksh in some ways.
 
 
 

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?

John.

2. Newbie to Linux with Modem Problems

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

4. Is there a Linux App = Room Scheduling Program ?

5. ksh - checking for filenames matching a pattern

6. PPP server problem.

7. KSH Pattern Match Deletions

8. Xplanet rules

9. ksh pattern matching

10. ksh conditional (string match pattern)

11. ksh and pattern matching

12. How can I pattern-match in ksh?

13. my fault about pattern matching in ksh