-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop_this
executable file
·54 lines (49 loc) · 1.24 KB
/
loop_this
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
#!/usr/bin/env bash
#
# Public: Executes a given command in a given number of times.
#
# - size: number of times that the command will be executed.
# - command: the command that will be executed.
#
# Modifiers:
#
# DONT_STOP: if this environment variable is set, the given
# command will be execute in the given times, even if it does not
# return success.
#
# Examples
#
# # will execute the `ls` command 5 times.
# loop_this 5 ls
#
# # will execute the `rspec` 10 times, even if the command return something
# # different from 0.
# DONT_STOP=1 loop_this 10 rspec
###############################################################################
params=($(echo "$@"))
cmd="${params[@]:1}"
size=${params[0]}
build_count=0
error_count=0
cmd=$(echo $cmd | sed "s/'/'\\''/g")
report() {
echo -e "\n\nkilled: << Build $build_count (failed: $error_count) >>\n\n"
exit $error_count
}; trap report SIGINT SIGTERM
while [ $build_count -lt $size ]
do
echo ">> Build $((++build_count)) (failed: $error_count)"
echo ">> $cmd"
$cmd
if [[ $? != 0 ]]
then
error_count=$(($error_count + 1))
if [[ "$DONT_STOP" = "" ]]
then
break
fi
fi
echo ""
done
echo -e "\nBuilds: $build_count (failed: $error_count)\n"
exit $error_count