comparison .shell.d/00.path_manipulation.sh @ 135:de6d0a9a7e3f

path_manipulation: more flexible so it can be used on non-PATH items
author Augie Fackler <durin42@gmail.com>
date Tue, 18 Aug 2009 13:43:57 -0500
parents 62bfb554ab85
children 7dfb7799567f
comparison
equal deleted inserted replaced
134:fe9d358b9fe8 135:de6d0a9a7e3f
1 # Functions for manipulating $PATH. Split out so I can use them in .zshenv if I want. 1 # Functions for manipulating $PATH. Split out so I can use them in .zshenv if I want.
2 2
3 function insert_path_element() { 3 function insert_path_element() {
4 if echo "$PATH" | grep "$1:" >> /dev/null; then 4 insert_element_into_var $1 PATH
5 true
6 else
7 export PATH="$1:$PATH"
8 fi
9 } 5 }
10 6
11 function remove_path_element() { 7 function remove_path_element() {
12 if echo "$PATH" | grep "$1:" >> /dev/null; then 8 remove_path_element $1 PATH
13 export PATH="`echo $PATH | sed s%$1:%%`"
14 else
15 echo $1 not in PATH
16 fi
17 } 9 }
10
11 function insert_element_into_var () {
12 eval varcontents=\$"$2"
13 eval varname="$2"
14 newcomp="$1"
15 if echo "$varcontents" | grep "$newcomp:" >> /dev/null; then
16 true
17 elif [[ "$varcontents" == "" ]] ; then
18 export $varname="$newcomp"
19 else
20 export $varname="$newcomp:$varcontents"
21 fi
22 }
23
24 function remove_element_from_var() {
25 eval varcontents=\$"$2"
26 eval varname="$2"
27 newcomp="$1"
28 if echo "$varcontents" | grep "$newcomp:" >> /dev/null; then
29 export $varname="`echo $varcontents | sed s%$newcomp:%%`"
30 elif [[ "$varcontents" == "$newcomp" ]] ; then
31 unset $varname
32 else
33 echo $1 not in $varname
34 fi
35 }