-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_fields.sh
42 lines (40 loc) · 1.41 KB
/
.bash_fields.sh
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
# SYNOPSIS: field_prepend varName fieldVal [sep]
# SEP defaults to ':'
# Note: Forces fieldVal into the first position, if already present.
# Duplicates are removed, too.
# EXAMPLE: field_prepend PATH /usr/local/bin
field_prepend() {
local varName=$1 fieldVal=$2 IFS=${3:-':'} auxArr
read -ra auxArr <<< "${!varName}"
for i in "${!auxArr[@]}"; do
[[ ${auxArr[i]} == "$fieldVal" ]] && unset auxArr[i]
done
auxArr=("$fieldVal" "${auxArr[@]}")
printf -v "$varName" '%s' "${auxArr[*]}"
}
# SYNOPSIS: field_append varName fieldVal [sep]
# SEP defaults to ':'
# Note: Forces fieldVal into the last position, if already present.
# Duplicates are removed, too.
# EXAMPLE: field_append PATH /usr/local/bin
field_append() {
local varName=$1 fieldVal=$2 IFS=${3:-':'} auxArr
read -ra auxArr <<< "${!varName}"
for i in "${!auxArr[@]}"; do
[[ ${auxArr[i]} == "$fieldVal" ]] && unset auxArr[i]
done
auxArr+=("$fieldVal")
printf -v "$varName" '%s' "${auxArr[*]}"
}
# SYNOPSIS: field_remove varName fieldVal [sep]
# SEP defaults to ':'
# Note: Duplicates are removed, too.
# EXAMPLE: field_remove PATH /usr/local/bin
field_remove() {
local varName=$1 fieldVal=$2 IFS=${3:-':'} auxArr
read -ra auxArr <<< "${!varName}"
for i in "${!auxArr[@]}"; do
[[ ${auxArr[i]} == "$fieldVal" ]] && unset auxArr[i]
done
printf -v "$varName" '%s' "${auxArr[*]}"
}