简体   繁体   中英

Format bash-script output

I'm searching for a while now if theres a special format-argument for printf or echo . I always see this kind of output when i start my ubuntu. My goal is to print a text like this:

Setting up locals                  [DONE]
Starting simulation                [DONE]
Receiving Data                     [FAIL]
Retry operation                      [OK]

The information in brackets should always be aligned on the right border of the terminal and colored.

Can someone give me a hint, how i can arrange that?

Thank you and BR

The following bash will align the Severity to the right of the terminal.

log_msg() {
    MSG="$1"
    SEVERITY="[$2]"
    let COL=$(tput cols)-${#MSG}

    printf "%s%${COL}s" "$MSG" "$SEVERITY"
}

Usage:

log_msg "Setting up locals" "DONE"
log_msg "Starting simulation" "DONE"
log_msg "Receiving Data" "FAIL"
log_msg "Retry operation" "OK"

You can try something like this:

### Config ###
red="\033[91m"
green="\033[92m"
yellow="\033[93m"
blue="\033[94m"
viol="\033[95m"
cyan="\033[96m"
none="\033[0m"

printf "$viol%40s$none" "$testMessage..."
# execute test here 
/path/test > output 2>&1
ret=$?
if [ $ret -eq 0 ]; then
    printf "$green%10s$none\n" "[PASS]"
else
    printf "$red%10s$none\n" "[FAILED]"
fi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM