Solved

Is there a Korn shell data type equivlent to Pascal sets?

Posted on 2013-01-09
6
276 Views
Last Modified: 2013-01-18
I know! It's just wishful thinking. Just thought I'd put it out there.

I have a situation for which, if I could use Pascal, I'd define a set type and declare a set variable which I could reference in a construct similar to the following...

SOMETHING = (me, myself, I)
SET = set of SOMETHING
THIS: SET
THAT: SET
readln(THIS)
if THIS in THAT; then...

My Pascal is a little rusty, but...

What I'm trying to do is determine whether one number is included in a set of other numbers. So far, all I've come up with is the case statement:

case $NUMBER in
  1|5|9|21|34) Do something or do nothing, as it were...
0
Comment
Question by:babyb00mer
6 Comments
 
LVL 31

Expert Comment

by:farzanj
Comment Utility
How about?

set="1 5 9 21 34"
if $(echo $set | grep -q "\b$NUMBER\b")
then
     echo do something
fi

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
SET='^(1|5|9|21|34)$'
[[ $NUMBER =~ $SET ]] && Do something

SET=" 1 5 9 21 34 "
[[ $SET =~ " $NUMBER " ]] && Do something
0
 

Author Comment

by:babyb00mer
Comment Utility
Hmm. I've never seen the '=~' operator. Unfortunately, it doesn't look like my versions of the Korn shell will accept it. I apologize. I forgot to mention that I'm running on AIX 5.3 and 6.1.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 84

Expert Comment

by:ozo
Comment Utility
Does your version of ksh support this?

SET=" 1 5 9 21 34 "  
[[ ${SET/ $NUMBER } != $SET ]] && Do something
0
 
LVL 68

Expert Comment

by:woolmilkporc
Comment Utility
SET=" 1 5 9 21 34 "  
NUMBER=34
if [[ $(expr index "$SET" "$NUMBER") -ne 0 ]] then
 echo Found. Do something.
   else
      echo Not found. Do nothing.
fi

or

SET=" 1 5 9 21 34 "
NUMBER="34"
if [[ "$SET" == *"$NUMBER"*  ]] then
 echo Found. Do something.
   else
      echo Not found. Do nothing.
fi

Unfortunately, the above will also match with e. g. NUMBER="3" (but at least the second one will not match with NUMBER=" 3 "), so I think farzanj's solution is best (so far), but without "\b", because our grep doesn't understand this syntax. Use "-w" (word match) instead:

... grep -wq "$NUMBER" ...
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
SET=" 1 5 9 21 34 "
[[ "$SET" == *" $NUMBER "*  ]]
 will not match with NUMBER="3"
nor will
SET="1|5|9|21|34"
[[ $NUMBER == $SET  ]]
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

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