-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_exe_until_fail.sh
executable file
·86 lines (75 loc) · 2.2 KB
/
run_exe_until_fail.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
81
82
83
84
85
86
#!/usr/bin/env bash
# runs the executable $1 until it returns with a non-0 exit status, and,
# when it fails, prints the executable's stdout output, stderr output,
# and exit status, and then exits with that exit status
# checks args and reads in the number of times to run
function parse_input() {
# correct number of args
if [[ "${#}" -le 2 ]]
then
# correct arg format
if [[ "${#}" = 2 && "${2}" =~ ^[1-9][0-9]*$ || "${#}" = 1 ]]
then
if [[ "${#}" = 2 ]]
then
readonly RUN_LIMIT="${2}"
else
readonly RUN_LIMIT=999 # run 999 times by default
fi
return 0
fi
fi
return 1
}
function print_usage() {
echo "Usage: ${0} <executable> [times]"
echo -e "\nruns executable the given number of times (or 999 times if unspecified)"
}
# runs $1 until it fails
function run_exe_until_fail()
{
for (( RUN_COUNT = 1; RUN_COUNT <= RUN_LIMIT; ++RUN_COUNT ))
do
echo -ne "\rRun #${RUN_COUNT}... "
if EXE_OUTPUT="$("${1}" 2>&1)"; then
: # Do nothing here cuz inverting the conditional would make
# us lose the exit status.
else
local EXE_EXIT_STATUS="${?}"
echo "exe failed with exit code ${EXE_EXIT_STATUS}!"
echo "==== exe output start =========================================================="
echo "${EXE_OUTPUT}"
echo "============================================================ exe output end ===="
exit "${EXE_EXIT_STATUS}"
fi
done
echo ""
}
if parse_input "${@}"
then
run_exe_until_fail "${1}"
echo "No failure after ${RUN_LIMIT} runs."
exit 0
else
print_usage
exit 1
fi
# For example, call this with something like this Python script:
# #!/usr/bin/env python3
# import random
# import sys
# import time
#
# def coinFlip():
# return random.choice([True, False])
#
# if coinFlip() == True:
# print("(STDOUT) exec success")
# time.sleep(1)
# sys.exit(0)
# else:
# print("(STDOUT) exec FAIL")
# sys.stdout.flush()
# sys.stderr.write("(STDERR) failure info...")
# sys.stderr.flush()
# sys.exit(1)