Removing duplicate PATH entries

Seemed like everywhere people just kept adding things to PATH without regards to whether it was already in PATH. I don't suspect that long PATH entries are a performance problem in Linux and Solaris, but Windows??? I don't pretend to completely understand all the places in a Windows system where PATH is processed and repeatedly scanned and the directories repeatedly probed, but it seemed like an easy thing to fix for all platforms, ... or so I thought. So I investigated how I could remove duplicate entries in the PATH variable using some kind of shell commands, I ended up with an awk command that worked pretty well.

Below I added newlines in the single quote argument to awk, you may need to mush those lines together into one long line, I broke it up below so it was easier to read.


# Given a PATH like string and a separator, remove duplicate entries
removeDups() # string sep
{
  if [ "${osname}" = "windows" ] ; then
    printf "%s\\n" "$1" | \\
      sed -e 's@\\\\@/@g' | \\
      ${AWK} -F"$2" \\
       '{ \\
          a[toupper($1)]; \\
          printf "%s",$1; \\
          for(i=2;i<=NF;i++){ \\
            if(!(toupper($i) in a)){ \\
              a[toupper($i)]; \\
              printf "%s%s",FS,$i; \\
            } \\
          }; \\
          printf "\\n"; \\
        }'
  else
    printf "%s\\n" "$1" | \\
      ${AWK} -F"$2" \\
       '{ \\
          a[$1]; \\
          printf "%s",$1; \\
          for(i=2;i<=NF;i++){ \\
            if(!($i in a)){ \\
              a[$i]; \\
              printf "%s%s",FS,$i; \\
            } \\
          }; \\
          printf "\\n"; \\
        }'
  fi
}

# OS name: Linux or SunOS, pot luck on Windows
osname="`uname -s`"
sep=":"
if [ `printf "%s\\n" "${PATH}"` != "" ] ; then
  osname="windows"
  sep=";"
fi
if [ "`echo ${osname} | grep -i CYGWIN`" != "" ] ; then
  osname="windows"
fi

# Need particular AWK
AWK=awk
if [ "${osname}" = "SunOS" ] ; then
  AWK=nawk
fi

# Get new path setting
newpath=`removeDups "${PATH}" "${sep}"`

# Redefine your PATH setting
if [ "${PATH}" != "${newpath}" ] ; then
  echo "# Resetting PATH to remove duplicates"
  PATH_ORIGINAL="${PATH}"
  export PATH_ORIGINAL
  PATH="${newpath}"
  export PATH
fi

Maybe someone else can get something out of this. Or suggest an even better way. ;\^)

-kto

Comments:

Post a Comment:
Comments are closed for this entry.
About

Various blogs on JDK development procedures, including building, build infrastructure, testing, and source maintenance.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today