various improvements
[distro-setup] / path_add-function
1 #!/bin/bash
2 # no bashisms so it can be used in debian profile run by dash
3 # --start adds to start of path, which will give it highest priority
4 # --ifexists will add to path only if the directory exists
5 path_add() {
6 local found x y z
7 local ifexists start
8 ifexists=false
9 start=false
10 while [ "$1" = --* ]; do
11 if [ "$1" = --start ]; then
12 start=true
13 elif [ "$1" = --ifexists ]; then
14 ifexists=true
15 fi
16 shift
17 done
18 for x in "$@"; do
19 found=false
20 IFS=:
21 for y in $PATH; do
22 [ "$x" = "$y" ] && found=true
23 done
24 unset IFS
25 if ! $found; then
26 if ! $ifexists || [ -d $x ]; then
27 if [ ! "$PATH" ]; then
28 PATH="$x"
29 elif $start; then
30 PATH="$x:$PATH"
31 else
32 PATH="$PATH:$x"
33 fi
34 fi
35 fi
36 done
37 }