LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 06-15-2005, 06:15 PM   #1
tongar
Member
 
Registered: Jan 2005
Distribution: SUSE ES 9 for x86
Posts: 43

Rep: Reputation: 15
Bash way to tell if String is in String


env: SLES9 SP1 intell, KDE

Hi,

I think I am in BASH, I am in the /etc/profile.local that I created, but I did not put #!/bin/bash at the top, so who knows what it is.

Anyway, I want to export PATH with an appended dir in there. Problem is, I need to know if it is already appended, because it gets appended twice. So how would I do this? I have tried "expr index...", grep wants a file, must be a way. If someone could please post the actual code instead of speaking in vague abstractions, that would be great. A coworker gave me a huge example script involving large functions and arrays and a main, but isn't there an easy way.

thanks,
tongar
 
Old 06-15-2005, 06:20 PM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Re: Bash way to tell if String is in String

Quote:
Originally posted by tongar
env: SLES9 SP1 intell, KDE

grep wants a file, must be a way.
You can pipe ( | ) to grep whatever you like:
Code:
 echo "$PATH" |grep <pattern>
 
Old 06-15-2005, 06:57 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Anyway, I want to export PATH with an appended dir in there. Problem is, I need to know if it is already appended, because it gets appended twice. So how would I do this?
It should be quite possible to prevent a directory from being include in $PATH more than once without some sort of special construct. Just read the man page about bash how it read its profile and rc files. Then just don't include some directory twice in $PATH.

But if you want it anyway, try this:
Code:
DIR_TO_ADD="/example/dir/bin"
echo "$PATH" | fgrep -qs $DIR_TO_ADD || export PATH=$PATH:$DIR_TO_ADD
Now the directory in $DIR_TO_ADD isonly added to $PATH if it's not already there.
 
Old 06-16-2005, 07:59 AM   #4
tongar
Member
 
Registered: Jan 2005
Distribution: SUSE ES 9 for x86
Posts: 43

Original Poster
Rep: Reputation: 15
thanks perfect_circle and Hko,

Hko, the code you suggest works fine.

I read the man pages. It seems to indicate the etc/profile (which invokes etc/profile.local) would be the place for this. When I remove it from there I get no such dir in the PATH, when I put it in without your snippet, I get two after system startup and root login.

thanks for the help,
tongar
 
  


Reply


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
String in Bash Shell nguyenzoro Linux - Newbie 4 11-19-2005 05:49 PM
[Bash] Concatenate string using awk senorsnor Programming 7 05-05-2005 01:38 AM
bash + string manipulation dave bean Programming 7 02-16-2005 12:16 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 02:39 PM
BASH string expansions Barbarian Programming 2 11-21-2001 11:40 PM


All times are GMT -5. The time now is 03:38 PM.

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