1 |
On 21/05/06, Kevin O'Gorman <kogorman@×××××.com> wrote: |
2 |
> Does anyone know a nice little idiom for de-duping a colon-list like PATH |
3 |
> or MANPATH? |
4 |
|
5 |
Yeah this is something that constantly annoyed me, I forget where I |
6 |
found this (although I moved it to a function), it is not of my |
7 |
creation. |
8 |
|
9 |
To ensure no confusion with the below paste, I have additionally |
10 |
placed this in a text file: |
11 |
http://kutzooi.co.uk/cleanpath.sh.txt |
12 |
|
13 |
function cleanpath { |
14 |
# Removes duplicates from PATH style variables |
15 |
local variable='PATH' |
16 |
if [ $# -eq 1 ] |
17 |
then |
18 |
variable="$1" |
19 |
fi |
20 |
local var="${1:-${variable}}" oldpath newpath=: entry |
21 |
oldpath="${!var}:" |
22 |
while [ -n "$oldpath" ]; do |
23 |
entry="${oldpath%%:*}" |
24 |
oldpath="${oldpath#*:}" |
25 |
[ "${entry:0:1}" = / ] && [ -n "${newpath##*:$entry:*}" ] && \ |
26 |
[ -d "$entry" ] && newpath="$newpath$entry:" |
27 |
done |
28 |
newpath="${newpath#:}" |
29 |
eval "$var"'="${newpath%:}"' |
30 |
} |
31 |
|
32 |
cleanpath # defaults to PATH |
33 |
cleanpath MANPATH |
34 |
cleanpath LD_LIBRARY_PATH |
35 |
|
36 |
Hope this helps. |
37 |
Mark |
38 |
|
39 |
-- |
40 |
gentoo-user@g.o mailing list |