1847123 Members
5882 Online
110263 Solutions
New Discussion

setting PATH

 
Angela Swyers_1
Frequent Advisor

setting PATH

I have a script that adds to a path, but it adds it everytime that I run the script. How do I make it so that the script checks the path first and if the directory that I want to add is already in the path I don't want it to set anything.
10 REPLIES 10
John Poff
Honored Contributor

Re: setting PATH

Hi,

I wouldn't worry about it. Instead, you could just figure out exactly what you need in your PATH to run your script and put just those directories into your PATH in your script. Don't worry about what is or isn't there already, just set what you need and run it. It will be cleaner, easier, and much more secure.

JP
Victor Fridyev
Honored Contributor

Re: setting PATH

Hi,

You can do something like this:
#!/usr/bin/sh
pchk(){
if echo $1|sed 's/:/\
/g'|sed 's/$/:/
s/^/:/' |grep :${2}: 1>/dev/null ; then
return 1
else
return 0
fi
}
#-------------------------------------------------------------
export ORACLE_HOME=/home/oracle/orahome
#-------------------------------------------------------------
if [ -z "$SHLIB_PATH" ]; then
export SHLIB_PATH=$ORACLE_HOME/lib
else
if pchk $SHLIB_PATH $ORACLE_HOME/lib ;then
export SHLIB_PATH=$ORACLE_HOME/lib:$SHLIB_PATH
fi
fi

HTH
Entities are not to be multiplied beyond necessity - RTFM
Angela Swyers_1
Frequent Advisor

Re: setting PATH

This script is run over and over and it keeps adding the directory over and over to the path. My user said that it's causing problems. It complained about the PATH being too large.
John Poff
Honored Contributor

Re: setting PATH

Your PATH should get set once by the script when it starts running, and should be discarded when the script exits. I don't think you can pass the PATH back to the parent process. Does your script set the PATH inside of a loop? That could cause a problem. Even so, if you explicity set the path...

PATH=/usr/bin:/somepath

instead of adding to it...

PATH=${PATH}:/somepath

you won't have to worry about it.

JP
Marco Santerre
Honored Contributor

Re: setting PATH

Like John mentionned already, your PATH should be discarded as soon as the script is over.. but a quick way to check for your PATH would be :

NEWPATH=path

echo $PATH|grep $NEWPATH >/dev/null
if [ $? != 0 ]
then
export PATH=${PATH}:${NEWPATH}
else
echo "$NEWPATH is already in PATH"
fi

Cooperation is doing with a smile what you have to do anyhow.
RAC_1
Honored Contributor

Re: setting PATH

$(echo $PATH|tr ":" "\n"|grep "xxx") || export PATH=$PATH:/xxx

Anil
There is no substitute to HARDWORK
Jeroen Peereboom
Honored Contributor

Re: setting PATH

L.S.

As stated by another JP in this thread:
if you execute myscript by typing 'myscript' or '/.../myscript' or whatever, on exit of the script the PATH variable is unchanged. The script cannot change the PATH variable of its invoking / parent shell.

So one of the first commands of your script should be something like
PATH=/my/additional/path:$PATH or
PATH=$PATH:/my/additional/path.

Keep it simple!

JP

P.S. For completeness: only if you source a script (i.e. '. myscript') the variables are set in the current shell.
Mike Stroyan
Honored Contributor

Re: setting PATH

Here is a general method for removing duplicate
entries from a path. I have it in my .profile
so it can add directories that are not already
in the path but refrain from creating duplicates
for directories that were already set by
/etc/profile.

function reduce_path
{
#remove duplicate directories from a path
typeset -i i j
i=0
(
IFS=: #break up path at colon delimiters
for d in $1
do
j=0
while [[ $j -lt $i ]]
do
if [[ $d = ${newpath[$j]} ]]
then
#found a duplicate
break;
fi
j=$j+1
done
if [[ $j = $i ]]
then
#found no duplicate
newpath[$i]=$d
i=$i+1
fi
done
print "${newpath[*]}"
)
}

PATH="$PATH:$HOME/bin:/usr/local/bin/X11:/usr/local/bin:/usr/sbin:."
PATH=$(reduce_path "$PATH")
Ken Penland_1
Trusted Contributor

Re: setting PATH

in your script, instead of doing just a:

PATH=$PATH:/some/more/path

you could do:

PATH=`cat /etc/PATH`:/some/more/path

and have all the normal stuff in PATH be in the file /etc/PATH?

that way, instead of it adding to the old variable each time, it is redefining the variable each time.
'
Bill Hassell
Honored Contributor

Re: setting PATH

Another point of view is to NEVER inherit $PATH in your script. Instead, forget completely about what PATH contains and define it at the beginning of the script:

#!/usr/bin/sh
export PATH=/usr/bin:/usr/contrib/bin

PATH can actually be a big security risk or it can run commands from the wrong locations. Write your script and add to PATH as needed and you won't have any problems with PATH growing.

NOTE: If you scipt is being sourced, that is, run by the current shell using the . (dot) command, then you'll need to scan $PATH and append as needed. The reason that the dot command is different is that all scripts are run in a subshell and changes made in the subshell are not propagated back to the parent. But when a script is sourced, it is run in the current shell so changes to $PATH will remain.

Another method is to simply drop the requirement for PATH extension by hardcoding the pathname:

someprogram some_parameters

becomes:

/opt/appname/bin/someprogram some_parameters

and leave $PATH alone.


Bill Hassell, sysadmin