Multi if or or similar


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multi if or or similar
# 1  
Old 01-05-2013
Sun Multi if or or similar

I'm trying to code a line (ksh) without much success along the lines of :-

If [ hostname = red or black or green or yellow or blue ]
then
.....etc
fi

I know i could use the case statement but i'm hoping someone may know a one liner or a simple workaround.
# 2  
Old 01-05-2013
Code:
#!/usr/bin/ksh

hostname="${1}"

if [[ "${hostname}" = @(red|black|green|yellow|blue) ]] ; then
   echo "${hostname} - match"
else
   echo "${hostname} - no match"
fi


Last edited by vgersh99; 01-06-2013 at 11:57 AM.. Reason: change color to hostname - thanks, Don
These 2 Users Gave Thanks to vgersh99 For This Post:
# 3  
Old 01-05-2013
thanks it works perfectly, what do i need to read up on to better understand the logic behind it.
# 4  
Old 01-05-2013
Quote:
Originally Posted by vgersh99
Code:
#!/usr/bin/ksh

hostname="${1}"

if [[ "${hostname}" = @(red|black|green|yellow|blue) ]] ; then
   echo "${color} - match"
else
   echo "${color} - no match"
fi

Thanks for pointing out how to do this in the Korn shell. I assume you meant to use $hostname (or ${hostname}) in both places where you used ${color}.
# 5  
Old 01-06-2013
Quote:
Originally Posted by Don Cragun
Thanks for pointing out how to do this in the Korn shell. I assume you meant to use $hostname (or ${hostname}) in both places where you used ${color}.
yeah, sorry - color was the original variable name.
# 6  
Old 01-06-2013
Quote:
what do i need to read up on to better understand the logic behind it.
Read up on Korn shell regular expressions.
# 7  
Old 01-06-2013
Quote:
Originally Posted by mjf
Read up on Korn shell regular expressions.
Actually, that is extended pattern matching, not a regular expression.

Code:
       A  pattern-list	is  a list of one or more patterns separated from each
       other with a & or |.  A & signifies that all patterns must  be  matched
       whereas	|  requires  that only one pattern be matched.	Composite pat-
       terns can be formed with one or more of the following sub-patterns:
	      ?(pattern-list)
		     Optionally matches any one of the given patterns.
	      *(pattern-list)
		     Matches zero or more occurrences of the given patterns.
	      +(pattern-list)
		     Matches one or more occurrences of the given patterns.
	      {n}(pattern-list)
		     Matches n occurrences of the given patterns.
	      {m,n}(pattern-list)
		     Matches from m to n occurrences of  the  given  patterns.
		     If  m  is	omitted,  0  will be used.  If n is omitted at
		     least m occurrences will be matched.
	      @(pattern-list)
		     Matches exactly one of the given patterns.
	      !(pattern-list)
		     Matches anything except one of the given patterns.

man ksh93

---
@squrcles: Why can't you use a case statement?
Code:
case $hostname in 
  (red|black|green|yellow|blue) echo "match"
esac


Last edited by Scrutinizer; 01-06-2013 at 04:47 PM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

2. Programming

Multi head/multi window hello world

I am trying to write a large X app. I have successfully modified my xorg.conf to setup 4 monitors on an NVIDIA Quatro5200. I am trying to modify a simple hello world application to open a window on three of the four monitors. depending on the changes to loop the window creation section and event... (2 Replies)
Discussion started by: advorak
2 Replies

3. Shell Programming and Scripting

How to substract selective values in multi row, multi column file (using awk or sed?)

Hi, I have a problem where I need to make this input: nameRow1a,text1a,text2a,floatValue1a,FloatValue2a,...,floatValue140a nameRow1b,text1b,text2b,floatValue1b,FloatValue2b,...,floatValue140b look like this output: nameRow1a,text1b,text2a,(floatValue1a - floatValue1b),(floatValue2a -... (4 Replies)
Discussion started by: nricardo
4 Replies

4. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

5. Shell Programming and Scripting

Help with awk or something similar

i have a file like this: wedd01A1 1 wedd01A2 2 wedd01A3 1 wedd02A2 3 wedd02A3 4 wadd02A1 1 wadd02A2 5 wqdd01A1 3 wsdd01A3 1 i want out like this: A1 A2 A3 wedd01 1 2 1 wedd02 0 3 4 wadd02 1 5 0 wqdd01 3 0 0 wsdd01 0 0 1 (8 Replies)
Discussion started by: aydj
8 Replies

6. AIX

Similar OS to AIX

Is there any OS similar to AIX around but free? (5 Replies)
Discussion started by: agasamapetilon
5 Replies

7. UNIX for Dummies Questions & Answers

I need a Unix OS similar to HP-UX

Hi, I am trying to quickly learn Unix because I am going to be using HP-UX soon. Problem is I can't find download HP-UX. Does anyone know a Unix OS I can download that is more like HP-UX? I'm running windows vista at the moment. (3 Replies)
Discussion started by: budz
3 Replies

8. UNIX for Dummies Questions & Answers

Multi User Multi Task

Dear Experts Why we always hear that unix operating system is Multi User and Multi task. What does these two means. I have looked at some books and documents but couldn't find aclear explenation. Can we say Windows operating system is also multi user and multi task?? Thanks for your help in... (6 Replies)
Discussion started by: Reza Nazarian
6 Replies

9. UNIX for Dummies Questions & Answers

multi-file multi-edit

Good day! I am trying to learn how to use the "sed" editor, to perform multiple edits on multiple files in multiple directories. I have one script that tries to call up each file and process it according to the edits listed in a second script. I am using a small input text to test these, at... (12 Replies)
Discussion started by: kielitaide
12 Replies
Login or Register to Ask a Question