-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
68 lines (62 loc) · 1.43 KB
/
functions.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
#!/usr/bin/env bash
#!/bin/bash
coloredEcho () {
local exp=$1;
local color=$2;
if ! [[ $color =~ '^[0-9]$' ]] ; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
magenta) color=5 ;;
cyan) color=6 ;;
white|*) color=7 ;; # white or invalid color
esac
fi
tput setaf $color;
echo $exp;
tput sgr0;
}
exit_on_error () {
if [ ${1} != 0 ]; then
coloredEcho "${2}" red
exit ${1}
fi
}
retry () {
local RET=0
local N=1
local COMMAND=$1
local MAX=$2
local DELAY=$3
while true; do
${COMMAND} && RET=$? && break || {
if [[ ${N} -lt ${MAX} ]]; then
((N++))
RET=$?
coloredEcho "<<< Command failed. Attempt ${N}/${MAX}" red
sleep ${DELAY};
else
RET=$?
coloredEcho "<<< The command (${COMMAND}) has failed after ${N} attempts." red
break
fi
}
done
return ${RET}
}
confirm () {
# call with a prompt string or use a default
read -n 1 -r -p "${1:-Are you sure? [y/N]} " response
echo # move to a new line
case $response in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}