Solved

KSH Case statement pattern with space in

Posted on 2004-04-18
6
3,040 Views
Last Modified: 2013-12-26
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
Comment
Question by:r47463
6 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
word1\ word2)  ;;
wo*\ wo*)        ;;
0
 
LVL 48

Expert Comment

by:Tintin
Comment Utility
#!/bin/sh
a="word1 word2"

case "$a" in
"word1 word2")  echo matched ;;
*)                        echo does not match ;;
esac
0
 
LVL 3

Expert Comment

by:dtkerns
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:r47463
Comment Utility
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
 
LVL 3

Accepted Solution

by:
dtkerns earned 55 total points
Comment Utility
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
 
LVL 3

Expert Comment

by:dtkerns
Comment Utility
PS notice you have to use 'real' REs not (k)sh file expansion REs
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now