-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadd_to_a_path.sh
More file actions
109 lines (98 loc) · 2.24 KB
/
Copy pathadd_to_a_path.sh
File metadata and controls
109 lines (98 loc) · 2.24 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#! /bin/bash
#
# Based on a file found at https://github.com/jalanb/jab.sh/blob/master/add_to_a_path.sh
# See also ./add_to_path.py and ./add_to_path.test
#
#
# Gonna need python
#
#
# Once sourced there is one major command:
#
add_to_a_path_py () {
if type python3 >/dev/null 2>&1; then
python3 ~/jab/src/python/add_to_a_path.py "$@"
else
echo "python3 unavailable, cannot set path" >&2
fi
}
add_to_a_path () {
if [[ -z $2 ]]; then
echo "Usage: add_to_a_path <SYMBOL> <new_path>"
echo " e.g. add_to_a_path PYTHONPATH /path/to/pylibs"
else
local new_paths_=$(add_to_a_path_py "$@")
for word in "$@"; do
[[ $1 =~ - ]] || break
local shifts_=
[[ $1 == '-i' || $1 == '--index' ]] && shifts_=2
shift $shifts_
done
if [[ -n $new_paths_ ]]; then
eval $new_paths_
export $1
else
echo $PATH
echo "$? - Failed to add for '""$@""', got '$new_paths_'"
fi
fi
}
add_to_PATH () {
add_to_a_path PATH "$@"
}
add_dir_to_a_path () {
[[ -d "$2" ]] || return 1
add_to_a_path "$@"
}
show_value () {
local name_=${1-SHELL}
local value=${!name_}
if [[ -z "$value" ]]; then
echo \$$name_ is not set
else
local where=${2-bash}
printf "$where has set \$$name_ to\n\t$value\n"
fi
}
setter_arg () {
local result_=
[[ $1 =~ bash ]] && result_=1
[[ $1 =~ user ]] && result_=2
[[ $result_ ]] || return 1
echo $result_
return 0
}
quiet_opt () {
local result_=
[[ $1 =~ -[qQ] ]] && result_=1
[[ $result_ ]] || return 1
echo $result_
return 0
}
show_a_path () {
[[ $1 ]] || return 1
local setter_=bash
local quiet_= arg_= name_=
for arg_ in "$@"; do
quiet_=$(quiet_opt $arg_) && continue
setter=$(setter_arg $arg_) && continue
name_=$1
done
[[ $quiet_ ]] || echo "$setter_ has set \$$name_ to:"
(
local paths_=${!name_} path_=
IFS=":"
for path_ in $paths_; do
echo " $path_"
done
)
}
show_path () {
show_a_path PATH "$@"
}
path () {
show_path "$@" -q
}
show_ppath () {
show_a_path PYTHONPATH "$@"
}