On Mon, 2003-09-15 at 09:37, John Clarke wrote:

[nice summary snipped]

> To add a new directory into $PATH:
> 
>     PATH=$PATH:/new/directory
>     export PATH
> 
> or:
> 
>     PATH=/new/directory:$PATH
>     export PATH
> 
> depending upon whether you want it to be searched first or last.

I've found it best to drop PATH changes into /etc/profile.d.
For example /etc/profile.d/openoffice.sh

#
# Add OpenOffice Betas to command search path before production
binaries.
  
if [ -r /etc/local/addpath.sh ]
then
  . /etc/local/addpath.sh
  [ -d /usr/local/OpenOffice.org1.1.0/program ] && \
  PATH=`addpath front "/usr/local/OpenOffice.org1.1.0/program"
"${PATH}"`
  export PATH
fi

where addpath is

#!/bin/bash -c 'echo Use source rather than running && exit 1'
#
# addpath.sh  -- define addpath(), which adds an element to a path.
#
# addpath()
# Add an element to a path, but only if that element is not already in
the path.
#   $1 = "front" or "rear", case-sensitive.
#   $2 = element.
#   $3 = path.
# Returns new path.
#
# Examples:
#   PATH=`addpath front "/sbin" "${PATH}"`
#   CLASSPATH=`addpath rear "jext.jar" "${CLASSPATH}"`
 
function addpath() {
  # Field seperator for paths is a colon, this should be local
  # to this function or it will have side effects upon the caller.
  local IFS=":"
  # Set (value 0) if element $2 is found in path #3, unset (value 1)
otherwise.
  local found=1
  # Current element being examined.
  local p
 
  # Check parameters.
  [ -z "${1}" ] && return 1
  [ -z "${2}" ] && return 1
 
  # Trivial case.
  # Path $3 is empty, so element $2 will be sole element in new path.
  [ -z "${3}" ] && echo "${2}" && return 0
 
  # Check for trailing parameters, these might be present through a
  # error in failing to quote a path with spaces.
  [ ${#} -ne 3 ] && return 1
 
  # Set found if element $2 is in path $3
  for p in ${3}
  do
    if [ "${p}" == "${2}" ]
    then
      found=0
    fi
  done
 
  # Is element already in path?
  if [ $found -eq 0 ]
  then
    # Yes.  Path $3 is unaltered.
    echo "${3}"
  else
    # No.  Add element $2 to front or rear of path $3.
    if [ "${1}" == "front" ]
    then
      echo "${2}:${3}"
    elif [ "${1}" == "rear" ]
    then
      echo "${3}:${2}"
    else
      # $1 is not front or rear.
      return 1
    fi
  fi
 
  return 0
}
# EOF


-- 
SLUG - Sydney Linux User's Group - http://slug.org.au/
More Info: http://lists.slug.org.au/listinfo/slug

Reply via email to