generated from sdavids/sdavids-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loop.sh
executable file
·52 lines (38 loc) · 898 Bytes
/
loop.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
#!/usr/bin/env bash
# SPDX-FileCopyrightText: © 2024 Sebastian Davids <sdavids@gmx.de>
# SPDX-License-Identifier: Apache-2.0
set -Eeu -o pipefail -o posix
readonly delay="${1:?delay is required}"
readonly initial_delay="${2:?initial delay is required}"
readonly script="${3:?shell script is required}"
case $1 in
'' | *[!0-9]*)
echo "delay '$1' is not a positive integer or 0" >&2
exit 1
;;
esac
case $2 in
'' | *[!0-9]*)
echo "initial delay '$2' is not a positive integer or 0" >&2
exit 2
;;
esac
if [ ! -f "$3" ]; then
echo "'$3' does not exist" >&2
exit 3
fi
sleep "${initial_delay}"
while true; do
set +e
"${script}" "${@:4}"
exit_code="$?"
set -e
if [ "${exit_code}" -eq 0 ]; then
exit 0
fi
if [ "${exit_code}" -ne 100 ]; then
echo "${script} exit code ${exit_code}" >&2
exit "${exit_code}"
fi
sleep "${delay}"
done