-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfor-each
More file actions
57 lines (44 loc) · 995 Bytes
/
for-each
File metadata and controls
57 lines (44 loc) · 995 Bytes
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
#!/usr/bin/env bash
print_help() {
cat <<EOF
usage: for-each [--help|-h] '<commands>'
commands can use a special string \`{}\`, which will be replaced with
the name of the current component.
examples:
# single command example
for-each 'git status'
# multiple command example
for-each 'bower version path && git push origin --tags'
# conditional commands and use of \`{}\`
for-each 'if ! { git tag -l | grep "0.0.0"; } &> /dev/null; then echo "{}"; fi'
# a more complex example that shows the repo status more specifically
for-each 'printf "%-30s: %s\n" "{}" "$(git status | grep -oiE "untracked|modified|deleted" | head -n 1)"'
EOF
exit 1
}
if (($# < 1)); then
print_help
fi
for opt in "${@:1}"; do
case "$opt" in
--help)
print_help
;;
-h)
print_help
;;
*)
cmd="$opt"
;;
esac
done
for dir in t-*
do
if ! [[ -d "$dir" ]]; then
continue
fi
pushd "$dir" &>/dev/null
ex_cmd="$(sed -r "s:\{\}:$dir:gi" <<<"$cmd")";
eval "$ex_cmd"
popd &>/dev/null
done