about summary refs log tree commit diff stats
path: root/profile
diff options
context:
space:
mode:
Diffstat (limited to 'profile')
-rw-r--r--profile/00_functions.sh100
1 files changed, 51 insertions, 49 deletions
diff --git a/profile/00_functions.sh b/profile/00_functions.sh index a94eaee..3c67ddf 100644 --- a/profile/00_functions.sh +++ b/profile/00_functions.sh
@@ -2,61 +2,63 @@
2# these should be POSIX-compatible. 2# these should be POSIX-compatible.
3 3
4# add a path to PATH, but only if it's not already there 4# add a path to PATH, but only if it's not already there
5path_add_to_PATH() { # path_add [-a] PATH... 5path_add_to_PATH() { # path_add_to_PATH [-a] PATH...
6 # -a appends (default is prepend) 6 # -a appends (default is prepend)
7 APPEND=false 7 APPEND=false
8 if [ "x$1" = "x-a" ]; then 8 if [ "x$1" = "x-a" ]; then
9 APPEND=true 9 APPEND=true
10 shift 10 shift
11 fi 11 fi
12 12
13 for p; do 13 for p; do
14 case ":$PATH:" in 14 case ":$PATH:" in
15 *:"$p":*) ;; 15 *:"$p":*) ;;
16 *) 16 *)
17 if $APPEND; then 17 if $APPEND; then
18 PATH="$PATH:$p" 18 PATH="$PATH:$p"
19 else 19 else
20 PATH="$p:$PATH" 20 PATH="$p:$PATH"
21 fi 21 fi
22 ;; 22 ;;
23 esac 23 esac
24 done 24 done
25 25
26 unset APPEND 26 unset APPEND
27} 27}
28 28
29# Generalization of `path_add_to_PATH' for any variable. 29# Generalization of `path_add_to_PATH' for any variable.
30path_add_unsafe() { #path_add_unsafe [-a] [-d DELIM] VAR PATH... 30path_add_unsafe() { #path_add_unsafe [-a] [-d DELIM] VAR PATH...
31 ## Add PATH... to VAR, delimiting with DELIM (default :). 31 ## Add PATH... to VAR, delimiting with DELIM (default :).
32 # By default, the VAR will be prepended to; passing -a will append the 32 # By default, the VAR will be prepended to; passing -a will append the
33 # variable. -d DELIM defines the delimiter. 33 # variable. -d DELIM defines the delimiter.
34 # 34 #
35 # This function has the _unsafe suffix because it uses `eval' to set 35 # This function has the _unsafe suffix because it uses `eval' to set
36 # variables. 36 # variables.
37 APPEND=false; DELIM=: 37 APPEND=false
38 while getopts ad: opt; do 38 DELIM=:
39 case "$opt" in 39 while getopts ad: opt; do
40 a) APPEND=true ;; 40 case "$opt" in
41 d) DELIM="$OPTARG" ;; 41 a) APPEND=true ;;
42 *) return 1 ;; 42 d) DELIM="$OPTARG" ;;
43 esac 43 *) return 1 ;;
44 done 44 esac
45 shift $(expr $OPTIND - 1) 45 done
46 shift $(expr $OPTIND - 1)
46 47
47 var="$1"; shift 48 var="$1"
49 shift
48 50
49 for path; do 51 for path; do
50 case ":$(eval "echo \$$var"):" in 52 case ":$(eval "echo \$$var"):" in
51 *:"$path":*) ;; 53 *:"$path":*) ;;
52 *) 54 *)
53 if $APPEND; then 55 if $APPEND; then
54 eval "$var=\$$var${var:+$DELIM}$path" 56 eval "$var=\$$var${var:+$DELIM}$path"
55 else 57 else
56 eval "$var=$path${var:+$DELIM}\$$var" 58 eval "$var=$path${var:+$DELIM}\$$var"
57 fi 59 fi
58 ;; 60 ;;
59 esac 61 esac
60 done 62 done
61 unset -v APPEND DELIM var 63 unset -v APPEND DELIM var
62} 64}