The purpose of this repository is to store useful bash scripts.
- Terminal colors
- Logging (using above coloring script)
- Add a tesing utils script
This module stored in colors.sh
provides a color
function to easiy colorize text in terminal.
Use the color function as follow:
source colors.sh
color "<text>" "<styles>"
To color text, define foreground text color by using a style starting with fg-
source colors.sh
color "text in black" "fg-back"
color "text in green" "fg-green"
color "text in yellow" "fg-yellow"
color "text in blue" "fg-blue"
color "text in purple" "fg-purple"
color "text in cyan" "fg-cyan"
color "text in white" "fg-white"
will produce the following result:
To color background, define foreground text color by using a style starting with bg-
source colors.sh
color "text on black" "bg-back"
color "text on green" "bg-green"
color "text on yellow" "bg-yellow"
color "text on blue" "bg-blue"
color "text on purple" "bg-purple"
color "text on cyan" "bg-cyan"
color "text on white" "bg-white"
will produce the following result:
source colors.sh
color "normal" "normal"
color "bold" "bold"
color "low intensity" "low_intensity"
color "underline" "underline"
color "blink (not working)" "blink"
color "reverse" "reverse"
color "invisible (not working)" "invisible"
will produce the following result:
source ./colors.sh
color "Yellow on blue" "bg-blue fg-yellow"
color "Bold green" "bold fg-green"
color "Underline purple on yellow" "underline fg-purple bg-yellow"
will produce the following result:
By default, the coloring use tput
if available on your system. If not found, it will use the bash escaping \e
by default.
You can explicitely specify the colorer to use as follow:
source colors.sh
color "Use tput by default (or \e escaping if your system does not support tput)" "fg-blue"
color "Force use of \e escaping using --bash" "fg-blue --bash"
color "Force use of \033 escaping using --oct" "fg-blue --oct"
color "Force use of \x1b escaping using --hex" "fg-blue --hex"
will produce the following result:
Note: as github readme cannot render text color, note that the output examples featured in the following examples are colored.
source logging.sh
log warning "this is a warning"
log error "this is an error"
log critical "this is a critical"
will produce the following output
Using only the first letter of the log level will produce the same output.
source logging.sh
log w "this is a warning"
log e "this is an error"
log c "this is a critical"
will produce the same output as above
Using only the log level index will also produce the same output.
source logging.sh
log 3 "this is a warning"
log 4 "this is an error"
log 5 "this is a critical"
will produce the same output as above
The following log levels are availables:
- critical / c / 5
- error / e / 4
- warning / w / 3
- info / i / 2
- debug / d / 1
- verbose / v / 0
By default, the log level is set to WARNING.
source logging.sh
log warning "this is a warning"
log error "this is an error"
log critical "this is a critical"
log info "this is an info"
log debug "this is a debug"
log verbose "this is a verbose"
$ LOG_LEVEL=verbose ./myscript.sh
will produce the following output
You can also use logging function to update the log level in your code, for example
source logging.sh
# will not be printed (because log level is set to warning in default)
log info "hidden info"
log debug "hidden debug"
log verbose "hidden verbose"
# explicitely set log level to verbose for the following section
log_level verbose
log info "this is an info"
log debug "this is a debug"
log verbose "this is a verbose"
# explicitely set log level to info for the following section
log_level info
log info "this is an info"
log debug "hidden debug"
log verbose "hidden verbose"
# disable all logs
log_off
log critical "hidden critical"
log error "hidden error"
log warning "hidden warning"
log info "hidden info"
log debug "hidden debug"
log verbose "hidden verbose"
# reset log level to default (WARNING)
log_on
log info "hidden info"
log debug "hidden debug"
log verbose "hidden verbose"
source logging.sh
log warning "this is a warning"
log error "this is a error"
log critical "this is a critical"
$ LOG_PREFIX=0 ./myscript.sh
will produce the following output
#!/bin/bash
source ./logging.sh
log warning "this is a warning"
log error "this is a error"
log critical "this is a critical"
log_prefix 0
log warning "this is a warning"
log error "this is a error"
log critical "this is a critical"
$ ./myscript.sh
will produce the following output
By appending a number after your log level, you can add syling to your logs.
Indexes 1 to 5 generate headers by appending # before log.
#!/bin/bash
source ./logging.sh
log info "this is a normal info"
log info1 "this is a heading 1"
log i2 "this is a heading 2"
log 23 "this is a heading 3"
log i4 "this is a heading 4"
log info5 "this is a heading 5"
$ LOG_LEVEL=INFO LOG_PREFIX=0 ./myscript.sh
will produce the following output
Indexes 6 and 7 produce lists
#!/bin/bash
source ../logging.sh
log warning6 "item1 item2 item3"
log e7 "item1 item2 item3"
$ LOG_LEVEL=INFO LOG_PREFIX=0 ./myscript.sh
will produce the following output
In script, the following usage is recommended to prevent script to override
Example:
#!/bin/bash
# other.sh
source ./logging.sh
function my_func(){
local origin_log_level="${LOGGER_STATE[LOG_LEVEL]}"
local origin_log_prefix="${LOGGER_STATE[LOG_PREFIX]}"
log_level_safe "info" "0"
log i "info message only printed when my_func is called directly"
# restore log levels to settings of upper function
log_level_restore "${origin_log_level}" "${origin_log_prefix}"
}
#!/bin/bash
# myscript.sh
source ./logging.sh
source ./other.sh
log_level info
log i "enter in main script"
log_off
my_func
log_on
log i "Not printed because log_on reset to default (ERROR from env var)"
log_level i
log i "End"
calling $ LOG_LEVEL=ERROR LOG_PREFIX=0 ./myscript.sh
the result will be the following:
enter in main script
End