-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
errexit.sh
executable file
·121 lines (97 loc) · 1.48 KB
/
errexit.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bash
#
# Usage:
# ./16-errexit.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
fail() {
echo FAIL
false
}
ok() {
echo OK
true
}
_func() {
fail
ok
}
# Hm this is behavior is odd. Usually errexit stops at first failed command.
# || suppresses this functions and blocks!
#
# I guess it is sort of like "if { grep foo; grep bar } ?
test-func-or() {
_func || echo "Test function FAILED"
echo DONE # We get here
}
test-brace-or() {
{ fail
ok
} || {
echo "Test block FAILED"
}
echo DONE
}
test-func-if() {
if _func; then
echo THEN # shouldn't succeed!
else
echo ELSE
fi
echo DONE
}
test-brace-if() {
if { fail; ok; }; then
echo THEN # shouldn't succeed!
else
echo ELSE
fi
echo DONE
}
# This behaves as expected
test-func-pipe() {
_func | tee /dev/null
echo PIPE
}
test-brace-pipe() {
{ fail; ok; } | tee /dev/null
echo PIPE
}
# We get ELSE
test-fail-if() {
if fail; then
echo THEN
else
echo ELSE
fi
echo DONE
}
# We get ELSE
test-fail-or() {
fail || echo FAILED
echo DONE
}
succeed() {
return 0
}
fail() {
return 1
}
test-func-with-and() {
succeed && echo "OK 1"
fail && echo "OK 2" # Swallos the error because of errexit, not good!
succeed && echo "OK 3"
}
all() {
compgen -A function | egrep '^test-' | while read func; do
echo
echo "--- $func ---"
echo
set +o errexit
$0 $func
echo status=$?
set -o errexit
done
}
"$@"