summaryrefslogtreecommitdiffstats
path: root/README.md
blob: 026267540ff89ce12c4c97489ffc0801b016bac6 (plain)
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
# Bash-Lib

A simple bash library for your scripting needs

## Usage

Just `source` it at the start of your script.

It will provide you with some useful logging functions, and make your script
use [Bash's unofficial strict mode][strict-mode].

[strict-mode]: <http://redsymbol.net/articles/unofficial-bash-strict-mode/>

If you want your bash script to continue even in the event of errors, you can
put `set +e` after sourcing bash-lib.

## Logging

Bash-lib provides the usual `trace`, `debug`, `info`, `warn`, and `error`
logging functions. By default, `trace` and `debug` messages are not shown, set
the `BASH_LOG` variable to `1` or `2` to increase the verbosity.

You can also set the `BASH_LIB_NAME` variable to influence the verbosity
variable name. For example, `BASH_LIB_NAME=DEMO` will make bash-lib look for
the `DEMO_LOG` variable for logging verbosity.

The `fatal` functions is like `error`, but will `exit` the program with status
1.

The `critical` functions is like `error`, but will actually return with an
error. This is useful if you want to handle your errors with traps. See the
[demo](./demo.sh) for an example usage.

## Provided functions

- `echoe`: like `echo` but redirected to `stderr`
- `trace`: log a trace message
- `debug`: log a debugging message
- `info`: log an information message
- `warn`: log a warning message
- `error`: log an error message
- `fatal`: log an error message and `exit 1`
- `critical`: log an error message and return with an error
- `is_debug`: succeeds if debugging messages are enabled
- `is_trace`: succeeds if trace messages are enabled

## Provided variables

- `${VERBOSE_ARG}`: an array variable which is `("--verbose")` when debug
	messages are enabled, `()` otherwise. Use it by adding `${VERBOSE_ARG[@]}` in
	programs supporting the `--verbose` flag.

### Escape codes

- `${NORMAL}`: reset the styling
- `${BOLD}`

Foreground colors:

- `${RED}`
- `${GREEN}`
- `${YELLOW}`
- `${BLUE}`
- `${PURPLE}`
- `${CYAN}`
- `${WHITE}`