Elegant way for verbose mode in scripts?
As you noticed, you can define some log
functions like log
, log_debug
, log_error
, etc.
function log () { if [[ $_V -eq 1 ]]; then echo "$@" fi}
It can help increasing your main code readability and hide show\nonshow logic into logging function.
log "some text"
If _V
(global variable) is equal 1
"some text" will be printed, in other case it will not.
After reading all other posts I came up with this
# set verbose level to info__VERBOSE=6declare -A LOG_LEVELS# https://en.wikipedia.org/wiki/Syslog#Severity_levelLOG_LEVELS=([0]="emerg" [1]="alert" [2]="crit" [3]="err" [4]="warning" [5]="notice" [6]="info" [7]="debug")function .log () { local LEVEL=${1} shift if [ ${__VERBOSE} -ge ${LEVEL} ]; then echo "[${LOG_LEVELS[$LEVEL]}]" "$@" fi}
Then you can simply use it like this
# verbose error.log 3 "Something is wrong here"
Which will output
[error] Something is wrong here
#!/bin/bash# A flexible verbosity redirection function# John C. Petrucci (http://johncpetrucci.com)# 2013-10-19# Allows your script to accept varying levels of verbosity flags and give appropriate feedback via file descriptors.# Example usage: ./this [-v[v[v]]]verbosity=2 #Start counting at 2 so that any increase to this will result in a minimum of file descriptor 3. You should leave this alone.maxverbosity=5 #The highest verbosity we use / allow to be displayed. Feel free to adjust.while getopts ":v" opt; do case $opt in v) (( verbosity=verbosity+1 )) ;; esacdoneprintf "%s %d\n" "Verbosity level set to:" "$verbosity"for v in $(seq 3 $verbosity) #Start counting from 3 since 1 and 2 are standards (stdout/stderr).do (( "$v" <= "$maxverbosity" )) && echo This would display $v (( "$v" <= "$maxverbosity" )) && eval exec "$v>&2" #Don't change anything higher than the maximum verbosity allowed.donefor v in $(seq $(( verbosity+1 )) $maxverbosity ) #From the verbosity level one higher than requested, through the maximum;do (( "$v" > "2" )) && echo This would not display $v (( "$v" > "2" )) && eval exec "$v>/dev/null" #Redirect these to bitbucket, provided that they don't match stdout and stderr.done# Some confirmations:printf "%s\n" "This message is seen at verbosity level 3 and above." >&3printf "%s\n" "This message is seen at verbosity level 4 and above." >&4printf "%s\n" "This message is seen at verbosity level 5 and above." >&5