LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-13-2010, 02:23 PM   #1
someshpr
Member
 
Registered: Jul 2009
Location: CA, USA
Distribution: Debian, RHEL5.4, CentOS 5.4, 6.2, Ubuntu 11.04,12.04
Posts: 107

Rep: Reputation: 26
Find a substring/member in a string/array using bash


This may be a basic bash array/string operation related question, but I couldn't find any direct answer. So here it goes:

I have a lot of data sorted in various directories. All directories need same processing except for a special group of directories. I have a symbolic link of the script in discussion in each directory. I want the script to get the name of the current directory, check if that belongs to special group and do specific operations.

So I get the name of the directory
Code:
mm=`basename \`pwd\``
Now the the group of directories that needs something different to be done, contains these directories:
Code:
user member default  MIT qly ...
I was thinking if something like this is possible:

Code:
GRPS=(user member default  MIT qly ...)
if [[ $mm is a member of $GRPS array ]]; then
 echo "Special"
fi
So essentially I am looking for an operator/function that searches for a substring/member in a string/array and returns some indication of success or failure.

TIA,
 
Old 10-13-2010, 02:42 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
If I have understood correctly that the directories you want to treat differently are identified by containing a specific set of subdirectories, say user, member, default, MIT and qly then how about
Code:
if [[ -d user && -d member && -d default && -d MIT && -d qly]]; then
    <whatever you want to do in the special directories>
else
    <whatever you want to do in the other directories>
fi
 
Old 10-13-2010, 02:57 PM   #3
someshpr
Member
 
Registered: Jul 2009
Location: CA, USA
Distribution: Debian, RHEL5.4, CentOS 5.4, 6.2, Ubuntu 11.04,12.04
Posts: 107

Original Poster
Rep: Reputation: 26
Quote:
Originally Posted by catkin View Post
If I have understood correctly that the directories you want to treat differently are identified by containing a specific set of subdirectories, say user, member, default, MIT and qly then how about
Code:
if [[ -d user && -d member && -d default && -d MIT && -d qly]]; then
    <whatever you want to do in the special directories>
else
    <whatever you want to do in the other directories>
fi
Yes I can do that. In fact I have bypassed this issue earlier by doing that. But now I have a large number of special directories. So I was thinking if I could make the if statement less cumbersome and make it more modular (sort of).
 
Old 10-13-2010, 04:58 PM   #4
someshpr
Member
 
Registered: Jul 2009
Location: CA, USA
Distribution: Debian, RHEL5.4, CentOS 5.4, 6.2, Ubuntu 11.04,12.04
Posts: 107

Original Poster
Rep: Reputation: 26
Smile Found a way

I found a way to do that (hint):
Code:
if [[ "${GRPS}" == *${mm}* ]]; then
    <whatever I want to do in the special directories>
else
    <whatever I want to do in the other directories>
fi
This works now.
 
Old 10-14-2010, 02:17 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,564

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
Well you could do away with the asterixes:
Code:
if [[ "${GRPS}" =~ ${mm} ]]; then
 
  


Reply

Tags
array, bash, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash replace all matches of regex substring in string nickleus Linux - General 3 04-30-2011 11:08 AM
bash string to array patolfo Programming 16 05-17-2010 10:12 AM
i need to find a substring in a string and get the next 3 characters anurupr Linux - Newbie 8 03-07-2010 05:45 PM
Fast algorithm needed to find a substring in a string jlinkels Programming 13 10-11-2007 08:43 PM
bash: test for substring inside string ali_bush Linux - General 1 03-08-2007 04:34 PM


All times are GMT -5. The time now is 06:21 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration