Take the 2-minute tour ×

I have to write a bash script that makes lot of things. I'd like to print messages as nice as init scripts do. For example:

 Doing A... [OK]  
 Doing B... [ERROR]  

Do you know any way to make this?

Thanks in advance

share|improve this question
add comment

3 Answers

up vote 7 down vote accepted

On all my Linux boxes, the code to do that is in the file:


If you include that file (. /etc/init.d/functions) and then run your code doing this:

action /path/to/prog args

you will get the functionality you want.

share|improve this answer
thanks, I saw that file and the usage of action is: action $"Doing A..." /path/to/prog args. ;) thanks! –  Neuquino Mar 22 '10 at 19:27
My linux doesn't have this (xandros, a debian variant, on the eeePC). But having a look in /etc/init.d/*, the files seem to use /lib/lsb/init-functions in the same way, calling functions it defines, like log_begin_msg, log_progress_msg, log_warning_msg, log_failure_msg and log_end_msg. One of them also references /etc/init.d/functions, but only if the system is redhat. –  13ren Mar 23 '10 at 2:14
add comment

The init.d scripts follow a fairly easy to use template. Just find one and copy and modify it. The [OK] / [ERROR] stuff is done by a file that's "sourced" near the top of the file.

share|improve this answer
add comment

use printf. I like having things color coded too. :)

Here's the preamble I use in my scripts to setup the colors and a few printf statements...

# checkload.sh - script to check logs for errors.
# Created by Ryan Bray, rbray@xxx.com

set -e

# Text color variables
txtund=$(tput sgr 0 1)    # Underline
txtbld=$(tput bold)       # Bold
txtred=$(tput setaf 1)    # Red
txtgrn=$(tput setaf 2)    # Green
txtylw=$(tput setaf 3)    # Yellow
txtblu=$(tput setaf 4)    # Blue
txtpur=$(tput setaf 5)    # Purple
txtcyn=$(tput setaf 6)    # Cyan
txtwht=$(tput setaf 7)    # White
txtrst=$(tput sgr0)       # Text reset

And then I have statements like this that use colors in the output:

printf "Checking for descrepancies in $LOAD_DATE$ADD_COMP\n"
DELTAS=$(awk 'BEGIN { FS = "\"" } {print $24,$26,$28}' $COMP_FILE)
if [[ "$DELTAS" == *[1-9]* ]]; then
        printf "%74s[${txtred}FAIL${txtrst}]\n"
        printf "$COMP_FILE contains descrepancies.\n"
        exit 1
        printf "%74s[${txtgrn}PASS${txtrst}]\n"

Hope this helps!


share|improve this answer
add comment

Your Answer


