Make colours, escape sequences optional

Coloured output is tedious to parse, even unnecessary where the report
backend is not a terminal.

Signed-off-by: Peter <lusitania@users.noreply.github.com>
This commit is contained in:
lusitania 2015-09-16 17:43:00 +02:00 committed by Peter
parent dafd938d19
commit c8dded38aa
4 changed files with 32 additions and 5 deletions

View file

@ -51,4 +51,10 @@ cd docker-bench-security
sh docker-bench-security.sh sh docker-bench-security.sh
``` ```
Coloured output may be switched off to ease parsing:
```sh
sh docker-bench-security.sh -n |& grep '^\[WARN\]'
```
This script was build to be POSIX 2004 compliant, so it should be portable across any Unix platform. This script was build to be POSIX 2004 compliant, so it should be portable across any Unix platform.

View file

@ -18,6 +18,9 @@
this_path=$(abspath "$0") ## Path of this file including filenamel this_path=$(abspath "$0") ## Path of this file including filenamel
myname=$(basename "${this_path}") ## file name of this script. myname=$(basename "${this_path}") ## file name of this script.
# Defaults
set_colours default
export PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/sbin/ export PATH=/bin:/sbin:/usr/bin:/usr/local/bin:/usr/sbin/
# Check for required program(s) # Check for required program(s)
@ -37,6 +40,7 @@ usage () {
cat <<EOF cat <<EOF
usage: ${myname} [options] usage: ${myname} [options]
-n optional Don't colourise results
-h optional Print this help message -h optional Print this help message
-l PATH optional Log output in PATH -l PATH optional Log output in PATH
EOF EOF
@ -49,6 +53,7 @@ while getopts hl: args
do do
case $args in case $args in
h) usage; exit 0 ;; h) usage; exit 0 ;;
n) set_colour none;;
l) logger="$OPTARG" ;; l) logger="$OPTARG" ;;
*) usage; exit 1 ;; *) usage; exit 1 ;;
esac esac

View file

@ -46,3 +46,24 @@ get_command_line_args() {
tr "\0" " " < /proc/"$PID"/cmdline tr "\0" " " < /proc/"$PID"/cmdline
done done
} }
# Set terminal colours used by functions in output_lib
set_colours() {
case "$?" in
none)
bldred=
bldgrn=
bldblu=
bldylw=
txtrst=
;;
default|*)
bldred='\033[1;31m'
bldgrn='\033[1;32m'
bldblu='\033[1;34m'
bldylw='\033[1;33m' # Yellow
txtrst='\033[0m'
;;
esac
}

View file

@ -1,9 +1,4 @@
#!/bin/sh #!/bin/sh
bldred='\033[1;31m'
bldgrn='\033[1;32m'
bldblu='\033[1;34m'
bldylw='\033[1;33m' # Yellow
txtrst='\033[0m'
logit () { logit () {
printf "%b\n" "$1" | tee -a "$logger" printf "%b\n" "$1" | tee -a "$logger"