-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
57 lines (46 loc) · 1.38 KB
/
utils.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/echo "This should be sourced"
# This needs to remain shell-agnostic. It is used by
# both ZSH and Bash scripts. I typically use -u -e, which
# means that undefined variable can't be loaded.
get_config_value() {
local field_name=$1
# We assume the default config file is a local one.
local config_file=${2:-config}
if [[ ! -f $config_file ]]; then
echo "Config file $config_file doesn't exist" >&2
exit 2
fi
matches_count=$(grep -c -e "^$field_name: " $config_file || true)
if [[ $matches_count == "0" ]]; then
echo "Expected to find config value $field_name but found" >&2
echo "nothing. Does the file $config_file contain $field_name?" >&2
exit 1
elif [[ $matches_count -gt 1 ]]; then
echo "Expected to find only one value for $field_name but found " >&2
echo "many. Does the file $config_file contain $field_name?" >&2
exit 1
fi
# Now we've got the field, we can get the value out with awk.
awk -F': ' "/^$field_name:/ {print \$2}" $config_file
}
exportf() {
export $(echo $1)="`whence -f $1 | sed -e "s/$1 //" `"
}
list_descendants () {
children=$(ps -o pid= --ppid "$1")
echo $children
for pid in $children
do
list_descendants "$pid"
done
echo "$children"
}
list_descendents_from_proc() {
list_descendants $$
}
kill_children() {
echo "Killing children!"
children=$(list_descendants $$)
kill -9 $children || true
echo "Killed $children"
}