3

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

3
  • Have you tried looking at what does that printing for your computer's startup scripts? Commented Nov 9, 2015 at 19:13
  • Thanks for your answer, actually i have no idea where i find this script :( Commented Nov 9, 2015 at 19:44
  • Somewhere in upstart presumably though I don't know where as I don't know upstart at all. But ultimately it is just control codes to color the output and position the cursor. Commented Nov 9, 2015 at 19:52

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.