bash-beauty: formatted output for bash scripts

bash-beauty screenshot #1

screenshot #1


bash-beauty screenshot #2

screenshot #2



bash-beauty screenshot #3

screenshot #3


What is it?

Bash function library for displaying script progress output. The functions imitate the display format of Linux boot messages.

What does it do?

  • Logs/echo's script tasks and results in a columnar format
  • Appends each task line with a colored status result (terminal stdout only)
  • Makes your script's output look good and readable

Other Features

  • Customizable message column width
  • Quiet mode writes only to a specified log file
  • Optionally prefixes all messages with a 14 digit timestamp
  • Free

Requirements

GNU Bourne-Again Shell (BASH)

Functions

The bash-beauty.sh library has 5 printing functions:

  • printTask

    Prints a single task message padded with whitespace to default 80 characters.

    Usage

    printTask [OPTION]... MESSAGE
    

    Options

    -t     flag to automatically prepend message with 14 digit timestamp
    -l     specify log file to write to
    -q     quiet mode. Do not output to stdout, only write to log file if supplied
    -w     width of padded message column. Defaults to 80 characters.
    
  • printOk

    Prints a green OK status result [  OK  ]

    Usage

    printOk [OPTION]...
    

    Options

    -l     specify log file to write to
    -q     quiet mode. Do not output to stdout, only write to log file if supplied
    
  • printFail

    Prints a red failure status result [FAILED]

    Usage

    printFail [OPTION]... [MESSAGE]
    

    Options

    -l     specify log file to write to
    -q     quiet mode. Do not output to stdout, only write to log file if supplied
    
  • printWarn

    Prints a yellow warning status result [ WARN ]

    Usage

     printWarn [OPTION]...
    

    Options

    -l     specify log file to write to
    -q     quiet mode. Do not output to stdout, only write to log file if supplied
    
  • printInfo

    Prints a blue info status result [ INFO ]

    Usage

     printInfo [OPTION]...
    

    Options

    -l     specify log file to write to
    -q     quiet mode. Do not output to stdout, only write to log file if supplied
    

Example 1

Source the bash-beauty.sh file

. /path/to/bash-beauty.sh

Execute the printTask function with a description of your task, execute your code, and then execute an appropriate status function: printOk(), printFail(), printWarn(), printInfo()

printTask "Executing arbitrary code"
# your code here
printOk

Output

bash-beauty #1 example output

Example 2

If your tasks are commands that print output to stdout/stderr, then you'll probably want to capture their output to keep things looking nice. You may also want to examine each command's result code to determine the appropriate status to print.

This example script attempts to touch file /foo/bar. If successful, it prints a green status result [ OK ]. If unsuccessful, it prints a red failure status result [FAILED], the error encountered, and exits.

#!/bin/bash

# step 1 - source the function library
. ./lib/bash-beauty.sh

# step 2 - use printTask to display the task message
printTask "Attempting to touch /foo/bar"

# step 3 - execute the command,
# redirect command stderr to stdout,
# capture output to variable OUTPUT
OUTPUT=$(touch /foo/bar 2>&1)

# step 4 - examine the command's result code
if [ ! "$?" == 0 ]; then
  printFail "$OUTPUT"
  exit 1
fi
printOk

Output

bash-beauty example #2 output

Example 3

This example script prints to both the terminal and a log file, prefixes each task with a timestamp, and has a custom task message column width.

#!/bin/bash

. ./lib/bash-beauty.sh

# set up log file path variable
LOG_FILE="/tmp/foobar.log"

printTask -t -w 50 -l "$LOG_FILE" "Process running"
sleep 1s    # your code here
printOk -l "$LOG_FILE"

printTask -t -w 50 -l "$LOG_FILE" "Implementing and executing"
sleep 1s
printOk -l "$LOG_FILE"

printTask -t -w 50 -l "$LOG_FILE" "Doing stuff"
sleep 1s
printOk -l "$LOG_FILE"

printTask -t -w 50 -l "$LOG_FILE" "Gittin 'er done"
sleep 1s
printFail -l "$LOG_FILE" "Max hillbilly tolerance level reached"

Output to terminal

bash-beauty example #3 output

Contents of log file at /tmp/foobar.log

20091114161952  Process running                   [  OK  ]
20091114161953  Implementing and executing        [  OK  ]
20091114161954  Doing stuff                       [  OK  ]
20091114161955  Gittin 'er done                   [FAILED]

Max hillbilly tolerance level reached

Change Log

0.0.1 - 20091113

  • initial release