Find string in list with wildcards | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Find string in list with wildcards

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-25-2009
Johan III Johan III is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 25 June 2009, 7:37 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Find string in list with wildcards

I need to add code to a shell script to find out if a string matches any item in a list, where the list can contain a wildcard character.

For example, if I have the following list:
ok_versions="03-02-4, 04-01-*"

Then I want to compare a particular string to see if it matches any item in the list. For example, I could have a string like the following
my_version="04-01-2".

I want to check my_version against ok_versions, and note that my_version is ok if it is 03-02-4, 04-01-1, 04-01-2, etc., but that it is not ok if it doesn't match one of the patterns. The ok_versions list could have any number of entries (not just two).

I am new to shell scripting and would really appreciate anyone who can solve this problem for me.

Thanks!
Sponsored Links
    #2  
Old 06-25-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 21 October 2014, 6:12 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts

Code:
#!/bin/ksh
#set -x

my_version='04-01-2'

if [[ ${my_version} = +(04-01-*|03-02-4) ]]; then
   echo match
else
   echo no match
fi

a better question is ... why this does not work..
Anybody? This is on Solaris....

Code:
#!/bin/ksh
set -x

ok_versions="04-01-*|03-02-4"

my_version='04-01-2'

if [[ ${my_version} = +(${ok_versions}) ]]; then
   echo match
else
   echo no match
fi


Last edited by vgersh99; 06-25-2009 at 05:03 PM..
Sponsored Links
    #3  
Old 06-25-2009
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 October 2014, 2:03 AM EDT
Location: Zürich
Posts: 8,484
Thanks: 332
Thanked 1,047 Times in 914 Posts
I'm only going to guess (and it's probably not a good guess) that the ksh88 on Solaris is "older" (read as slightly different) than the ksh88 on whatever "nix you compared it to.

The ksh on AIX is not the same as on Solaris. There are subtle, but very minor differences (all of which I have momentarily - and somewhat conveniently forgotten!)
    #4  
Old 06-25-2009
Johan III Johan III is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 25 June 2009, 7:37 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I am working with a /bin/sh script file on a Linux nemi 2.4.25 PPC system.
Sponsored Links
    #5  
Old 06-25-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 21 October 2014, 6:12 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts
Quote:
Originally Posted by scottn View Post
I'm only going to guess (and it's probably not a good guess) that the ksh88 on Solaris is "older" (read as slightly different) than the ksh88 on whatever "nix you compared it to.

The ksh on AIX is not the same as on Solaris. There are subtle, but very minor differences (all of which I have momentarily - and somewhat conveniently forgotten!)
Do you have access to AIX? Could you try it, please.
It does not work with '/usr/xpg4/bin/sh' either ........

---------- Post updated at 05:32 PM ---------- Previous update was at 05:31 PM ----------

Quote:
Originally Posted by Johan III View Post
I am working with a /bin/sh script file on a Linux nemi 2.4.25 PPC system.
Your '/bin/sh' on Linux might actually be ksh or bash 'in disguise' - try it.
Sponsored Links
    #6  
Old 06-25-2009
Scott's Avatar
Scott Scott is offline Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 22 October 2014, 2:03 AM EDT
Location: Zürich
Posts: 8,484
Thanks: 332
Thanked 1,047 Times in 914 Posts
Hi Johan,

If you used vger's solution then you were really using ksh, if just for a moment!

vger... I don't have access to AIX at the moment... I'm home and only have Solaris, Linux. I can't afford to have AIX at home! But I'll check it tomorrow (out of interest).
Sponsored Links
    #7  
Old 06-25-2009
Johan III Johan III is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 25 June 2009, 7:37 PM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I tried vger's solution, but got the following errors:

syntax error in conditional expression: unexpected token `('
syntax error near `+($'
` if [[ ${my_version} = +(${ok_versions}) ]]; then'

I get the same kind of errors if I replace ok_versions with the string list in the if statement.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help--Using wildcards in string comparison deepae Shell Programming and Scripting 2 05-06-2009 01:01 PM
Find the position of a string and replace with another string bab123 Shell Programming and Scripting 6 01-21-2009 03:14 AM
Read a string with leading spaces and find the length of the string dayamatrix UNIX for Dummies Questions & Answers 2 11-13-2008 09:08 AM
counting a list of string in a list of txt files Pep Puigvert UNIX for Dummies Questions & Answers 4 08-15-2008 03:55 PM
command find returned bash: /usr/bin/find: Argument list too long yacsil Shell Programming and Scripting 1 12-15-2003 05:38 PM



All times are GMT -4. The time now is 04:01 AM.