-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.sh
executable file
·80 lines (69 loc) · 1.55 KB
/
lib.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /bin/sh
# contains STRING SUBSTRING
#
# Return success if SUBSTRING is contained into STRING.
#
contains () {
case "$1" in
*"$2"*) return 0 ;;
*) return 1 ;;
esac
}
# is_range SPEC
#
# Return success if SPEC is a valid range specification,
# i.e., has the form START-END.
#
is_range () {
contains "$1" '-'
}
# expand_range START-END
#
# Output all numbers from START to END (inclusive),
# one per line
#
expand_range () {
local start=$(echo "$1" | cut -d- -f1)
local end=$(echo "$1" | cut -d- -f2)
seq "$start" "$end" | tr ' ' '\n'
}
# expand_set SET
#
# Output all numbers in SET, one per line. SET is a
# comma-separated list of ranges START-END (inclusive).
#
expand_set () {
_expand_set_impl "$1" | tr -s '\n'; echo
}
_expand_set_impl () {
local set="$1"
if contains "$set" ','; then
local first=$(echo "$set" | cut -d, -f1)
local rest=$(echo "$set" | cut -d, -f2)
echo $(expand_set "$first")
echo $(expand_set "$rest")
else
if is_range "$set"; then
expand_range "$set"
else
# single number
echo "$set"
fi
fi
}
# for_each_online_cpu CMD ARG [ARG ...]
#
# Run the given command line, once per each online CPU.
# The literal string '{}' is substituted with the CPU
# number.
#
for_each_online_cpu () {
expand_set $(cat /sys/devices/system/cpu/online) \
| xargs --replace -n1 "$@"
}
# tests
expand_set '0'
expand_set '0-7'
expand_set '0-4,7'
expand_set '0-4,6-7'
for_each_online_cpu echo CPU '#{}'