-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpath.sh
87 lines (71 loc) · 1.66 KB
/
path.sh
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Functions that operate on PATH variables, and similarly formatted
# variables (LD_LIBRARY_PATH, CLASSPATH, etc)
#
# $Id$
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Clean one or more path variables of duplicate entries
function cleanpath
{
: ${1?'Missing name(s) of variables'}
typeset verbose=
#if interactive
#then
# verbose="-v"
#fi
verbose=
declare i=
for i in $*
do
eval $(pathclean $verbose -p $i)
done
}
# ---------------------------------------------------------------------------
# Remove an element from a path-like variable.
#
# Usage: rmpath pathvar element [verbose]
#
# "element" can be a wildcard pattern
function rmpath
{
typeset var=${1?'Missing name of variable'}
typeset val
typeset component=${2?'Missing path component to remove'}
typeset verbose=$3
typeset tokens
typeset path
eval val='$'$var
if [ -n "$verbose" ]
then
echo "Removing $component from $var"
fi
# Be sure to disable file name globbing during the set, since
# it's possible to have glob chars in the data.
set -o noglob
tokens=(${val//:/ })
set +o noglob
path=
let i=0
while [ $i -lt ${#tokens[*]} ]
do
if [ -n "$verbose" ]
then
echo "Checking ${tokens[$i]}"
fi
if [ "${tokens[$i]}" = "$component" ]
then
if [ -n "$verbose" ]
then
echo "*** match. Removing ${tokens[$i]}"
fi
else
path=$path:${tokens[$i]}
fi
let i=$i+1
done
if [ -n "$verbose" ]
then
echo $var=$path
fi
eval $var="$path"
}