LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Bash way to tell if String is in String (http://www.linuxquestions.org/questions/programming-9/bash-way-to-tell-if-string-is-in-string-334007/)

tongar 06-15-2005 06:15 PM

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

perfect_circle 06-15-2005 06:20 PM

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>

Hko 06-15-2005 06:57 PM

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.

tongar 06-16-2005 07:59 AM

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


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