LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Rep: Reputation: 28
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: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Original Poster
Rep: Reputation: 28
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: WI, USA
Distribution: Debian 8, Ubuntu 16.04, CentOS 7
Posts: 143

Original Poster
Rep: Reputation: 28
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
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

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


Reply

Tags
array, bash, string



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: test for substring inside string ali_bush Linux - General 3 04-26-2018 07:31 PM
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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:28 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration