diff options
Diffstat (limited to 'demo.sh')
-rw-r--r-- | demo.sh | 47 |
1 files changed, 47 insertions, 0 deletions
@@ -0,0 +1,47 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | |||
3 | readonly BASH_LIB_NAME=DEMO | ||
4 | source ./bash-lib.sh | ||
5 | |||
6 | # Set `DEMO_DEBUG=1` to enable debug messages | ||
7 | |||
8 | trace "this is a trace message" "with additional data" | ||
9 | debug "this is a debugging message" "with additional data" | ||
10 | info "this is an informational message" "with additional data" | ||
11 | warn "this is a warning message" "with additional data" | ||
12 | error "this is an error message" "with additional data" | ||
13 | |||
14 | # Like echo, but to stderr | ||
15 | echoe '------------------------------' | ||
16 | |||
17 | if is_debug; then | ||
18 | info "Debugging messages:" on | ||
19 | else | ||
20 | info "Debugging messages:" off | ||
21 | fi | ||
22 | |||
23 | if is_trace; then | ||
24 | info "Trace messages:" on | ||
25 | else | ||
26 | info "Trace messages:" off | ||
27 | fi | ||
28 | |||
29 | echoe '------------------------------' | ||
30 | |||
31 | # Use critical with `set +e` if you want to trap errors | ||
32 | ( | ||
33 | function handle_error() { | ||
34 | info "handling error..." | ||
35 | } | ||
36 | |||
37 | set +e | ||
38 | trap "handle_error" ERR | ||
39 | |||
40 | critical "this is a critical error:" "this will allow some error recovery" | ||
41 | info "but with 'set +e', the program continues its course" | ||
42 | ) | ||
43 | |||
44 | echoe '------------------------------' | ||
45 | |||
46 | info "use 'fatal()' in either case if you want to quit with an error message" | ||
47 | fatal "some unrecoverable error occurred, exiting" | ||