How to write a bash script like the ones used in init.d?

I need to write a bash script that does a lot of things. I would like to print messages as well as initialization scripts. For example:

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

Do you know how to do this?

Thanks in advance

+7
scripting bash
source share
3 answers

In all my Linux boxes, the code for this is in the file:

 /etc/init.d/functions 

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

 action /path/to/prog args 

You will get the necessary functionality.

+11
source share

The /etc/init.d/* scripts follow a fairly easy use of the template. Just find it and copy and modify.

The [OK] / [ERROR] is done by looking for the /etc/init.d/functions file in your script (at the top as a whole).

+4
source share

use printf. I like to have color code too. :)

Here's the preamble that I use in my scripts to adjust colors and multiple printf expressions ...

 #!/bin/bash # 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 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 else printf "%74s[${txtgrn}PASS${txtrst}]\n" fi 

Hope this helps!

-Ryan

+1
source share

All Articles