1833163 Members
3281 Online
110051 Solutions
New Discussion

Env Params

 
SOLVED
Go to solution
Shachar Twito_1
Occasional Contributor

Env Params

I have a program that needs certain env params to run (for example in this question I'll use "path"). During installation, it adds a line to the .login file stating set path = ( /a/b /c/d $path ). The problem is that sometimes the path was already defined with the "/c/d" directories before that line, therefore the path value will be path=/a/b /c/d /c/d.
I don't want this duplicity (there's the length limit besides it's not nice...).
Any ideas how to solve this mess (on unix in general)?

Thanks,
Shachar
4 REPLIES 4
Ruediger Noack
Valued Contributor

Re: Env Params

Hi,

you can test if your env variable already contains the new value with a simple script like this:

ADDPATH=YES
for i in `echo $PATH | tr ':' ' '`
do
[ "$i" = "$NEWPATH" ] && ADDPATH=NO
done
[ "$ADDPATH" = "YES" ] && PATH=$PATH:$NEWPATH

Hope this helps

Ruediger
Robin Wakefield
Honored Contributor

Re: Env Params

Hi Shachar,

How about this:

if $a contains the directory you're interested in:

echo $path | tr ' ' '\012' | grep -q -x $a || set path = ( $path $a )

will only add $a if it's not already there.

Rgds, Robin
Shachar Twito_1
Occasional Contributor

Re: Env Params

Thank you all for the answers.
Robin,
Does this line will work in any shell (sh, sh, tcsh, ksh...), and will it work on sun?
Shachar
Robin Wakefield
Honored Contributor
Solution

Re: Env Params

Hi Shachar,

The example I gave was for a csh. For sh/ksh, you'll need something like:

echo $PATH | tr : '\012' | grep -q -x $a || PATH=$PATH:$a;export PATH

For Sun, you'll have to use /usr/xpg4/bin/grep, since this supports the -q/-x switches.

Rgds, Robin.