changeset 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 fe9d358b9fe8
children 9b48ec89ffdf
files .shell.d/00.path_manipulation.sh
diffstat 1 files changed, 28 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/.shell.d/00.path_manipulation.sh
+++ b/.shell.d/00.path_manipulation.sh
@@ -1,17 +1,35 @@
 # Functions for manipulating $PATH. Split out so I can use them in .zshenv if I want.
 
 function insert_path_element() {
-    if echo "$PATH" | grep "$1:" >> /dev/null; then
-        true
-    else
-        export PATH="$1:$PATH"
-    fi
+  insert_element_into_var $1 PATH
 }
 
 function remove_path_element() {
-    if echo "$PATH" | grep "$1:" >> /dev/null; then
-        export PATH="`echo $PATH | sed s%$1:%%`"
-    else
-        echo $1 not in PATH
-    fi
+  remove_path_element $1 PATH
+}
+
+function insert_element_into_var () {
+  eval varcontents=\$"$2"
+  eval varname="$2"
+  newcomp="$1"
+  if echo "$varcontents" | grep "$newcomp:" >> /dev/null; then
+    true
+  elif [[ "$varcontents" == "" ]] ; then
+    export $varname="$newcomp"
+  else
+    export $varname="$newcomp:$varcontents"
+  fi
+}
+
+function remove_element_from_var() {
+  eval varcontents=\$"$2"
+  eval varname="$2"
+  newcomp="$1"
+  if echo "$varcontents" | grep "$newcomp:" >> /dev/null; then
+    export $varname="`echo $varcontents | sed s%$newcomp:%%`"
+  elif [[ "$varcontents" == "$newcomp" ]] ; then
+    unset $varname
+  else
+    echo $1 not in $varname
+  fi
 }