Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help modifying path on the fly

3 views
Skip to first unread message

Sidhe

unread,
Sep 25, 1999, 3:00:00 AM9/25/99
to
I need to change the users path on the fly without just appending the
new search path to the existing path.

We're running a 3rd party app called MTP. When entering MTP, an
environment script (korn) is executed that sets a series of variables
and then appends a path string "/app/mtm5411/prod/" to the current
path. This in and of itself is fine but we have 3 different versions of
this application. All you need to do to switch versions of the
application is change your root variable and re-execute the environment
script... until you get to the path section.

Obviously, after you've set the path once with this script, any
subsequent executions just append a new search path to the current path,
leaving the now invalid string in place.

So my question is this...

How can I check the path to see if we've already appended the MTP
directory to the path...
if NOT then go ahead and add our string to the current path
if TRUE then remove the now incorrect bit (can be one of 3 values) and
replace it with the correct string

The 3 possible values are as follows...
/app/mtm5411/MTPPROD/bin
/app/mtm5411/MTPTEST/bin
/app/mtm5411/MTPDEV/bin

Thanks,
Roger Westbrook
Dollar General Corp
rwest...@dollargeneral.com


Message has been deleted

Michael Wojcik

unread,
Sep 28, 1999, 3:00:00 AM9/28/99
to

[Followups set to comp.unix.shell.]

In article <37EC75FB...@home.net>, Sidhe <si...@home.net> writes:
> I need to change the users path on the fly without just appending the
> new search path to the existing path.

First, you need to post to a reasonable number of newsgroups, like
one. Crossposting is rude, and crossposting to six newsgroups is
grossly excessive. If you're not sure what group to post to, pick
one that seems relevant and post to it, with an invitation to
redirect followups.

This is a Korn shell question, so comp.unix.shell would have been
a good choice.

> [summary: Have the same executable in different directories, and
> want to switch which version gets run.]

ksh has some handy variable operations. In particular, there are
the "#" and "%" operators and their doubled versions, which strip
a minimal or maximal pattern match from the beginning or end of
an expansion. We can use them to make AddToPath and RemoveFromPath
functions:

AddToPath()
{
for dir in $@
do
if [[ $PATH != *:$dir:* && $PATH != $dir:* && $PATH != *:$dir ]]
then
PATH="$PATH:$dir"
fi
done
}

RemoveFromPath()
{
for dir in $@
do
if [[ $PATH = $dir:* ]]
then
PATH=${PATH#$dir:}
elif [[ $PATH = *:$dir ]]
then
PATH=${PATH%:$dir}
elif [[ $PATH = *:$dir:* ]]
then
PATH="${PATH%:$dir:*}:${PATH#*:$dir:}"
fi
done
}

(These aren't foolproof, but they're good enough.)

To add a couple of directories to the path:

AddToPath newdir1 newdir2

To remove one:

RemoveFromPath olddir1 olddir2

To change from olddir to newdir:

RemoveFromPath olddir
AddToPath newdir

(Changing PATH tosses any tracked aliases, so you shouldn't have to
worry about that.)


--
Michael Wojcik michael...@merant.com
AAI Development, MERANT (block capitals are a company mandate)
Department of English, Miami University

Advertising Copy in a Second Language Dept.:
The precious ovum itself is proof of the oath sworn to those who set
eyes upon Mokona: Your wishes will be granted if you are able to invest
it with eternal radiance...
-- Noriyuki Zinguzi

0 new messages