Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3081
  • Last Modified:

KSH Case statement pattern with space in

How can I get a pattern to have a space in, for use in a case statement? I have two words which I would like to use.

For example, this obviously doesn't work, but shows what I'm trying to do:

word1 word2)
  ...
  ;;

I would also like to use wild cards for this so that "wo wo" would work.

If this isn't possible, how can I use wildcards on a string in an if statement please?

Thanks

0
r47463
Asked:
r47463
1 Solution
 
ozoCommented:
word1\ word2)  ;;
wo*\ wo*)        ;;
0
 
TintinCommented:
#!/bin/sh
a="word1 word2"

case "$a" in
"word1 word2")  echo matched ;;
*)                        echo does not match ;;
esac
0
 
dtkernsCommented:
FYI
in ksh88i I was unable to get the following to work for a="word word"

"wo* wo*") ...
wo*\ wo*) ...

but both of these did:

"word word") ...
wo*) ...

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
r47463Author Commented:
Thanks all. Unfortunately so far I haven't been able to get any of these to work.

dtkerns - I'll try what you mentioned tonight.

At the moment the only way I can get it to work with two words is to use arguments, which doesn't seem like the right way to go about it, and doesn't allow me to use a wildcard on the string in an IF statement.
0
 
dtkernsCommented:
if you're only using the case for it's RE capabilities (as opposed to many if-else-if-else) you could use:

if expr "$a" : 'wo.* wo.*' > /dev/null
then
       echo true
else              
       echo false
fi                
0
 
dtkernsCommented:
PS notice you have to use 'real' REs not (k)sh file expansion REs
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now