My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
LackFunctions  
Documentation for the lack_functions.sh script
Updated Jan 24, 2012

Back to the MainPage...

lack_functions.sh

The lack_functions.sh script defines a set of shell functions and variables; most of the variables contain different parts of the ANSI escape sequences for colorizing text on a Linux console.

Instead of having to remember the characters that make up a specific ANSI sequence, you can just throw the shell variable in there and things will Just Work.

In order to use these in scripts, just source the lack_functions.sh script, then call any of the below shell variables as $VARIABLE in your scripts.

Commonly used strings

  • START - Start the ANSI "Set Graphics Mode" sequence (ESC[)
  • END - End the ANSI "Set Graphics Mode" sequence (m)
  • SUCCESS - The string " success."
  • FAILED - The string " failed."

Example usage, sets text foreground color to red then resets at the end of the line:

echo ${START}${FG_RED}${END}This is an example of "${FAILED}"${START}${NONE}${END}

Colorization shortcuts

Shell Variable Expands To Function
S_SUCCESS "${BOLD};${F_GRN};${B_BLK}" Bolded green text on a black background
S_FAILURE "${BOLD};${F_RED};${B_BLK}" Bolded red text on a black background
S_INFO "${F_BLK};${B_CYN}" Black text on a cyan background
S_TIP "${BOLD};${F_BLU};${B_BLK}" Bolded blue text on a black background
T_SUCCESS "${BOLD};${F_WHT};${B_GRN}" Bolded white text on a green background
T_FAILURE "${BLINK};${F_YLW};${B_RED}" Blinking yellow text on a red background
T_INFO "${INVERSE};${F_WHT};${B_BLU}" Inverse white text on a blue background???
T_TIP "${CONCEALED};${F_BLK};${B_WHT}" Concealed black text on a white background???

ANSI text sequences

Shell variables with pre-configured ANSI escape sequences for colorization.

Text attributes

Shell Variable Function Notes
NONE All attributes off
BOLD Bold text
NORM Normal text
BLINK Blinks text Does not work on all consoles/terminal emulators
INVERSE Reverses foreground and background colors
CONCEALED Concealed text

Background text colors

Shell Variable Color
B_BLK Black
B_RED Red
B_GRN Green
B_YLW Yellow
B_BLU Blue
B_MAG Magenta
B_CYN Cyan
B_WHT White

Foreground text colors

Shell Variable Color
F_BLK Black
F_RED Red
F_GRN Green
F_YLW Yellow
F_BLU Blue
F_MAG Magenta
F_CYN Cyan
F_WHT White

Note that not all color combinations are available on all consoles/terminal emulators; your best bet is to try something out first before you put it into production.

colorize ()

Arguments:

  1. ANSI text seqence(s)
  2. String to colorize

Colorizes a block of text, but does not output a newline at the end of the line.

Example usage:

colorize "$F_RED;$B_BLU" "This is red text on a blue background"

colorize_nl ()

Arguments:

  1. ANSI text seqence(s)
  2. String to colorize

Colorizes a block of text, adds a newline at the end of the line.

Example usage:

colorize_nl "$F_RED;$B_BLU" "This is red text on a blue background"

pause_prompt ()

Arguments:

  1. None

Checks for a $PAUSE_PROMPT variable defined in the shell environment, and if it exists, prompts the user to hit the <Enter> key prior to continuing script execution. This is used for debugging/troubleshooting, to prevent valuable text from scrolling off of the console buffer.

Example usage:

export PAUSE_PROMPT=1
pause_prompt

cmd_status ()

Arguments:

  1. Status code from last executed command; in KSH/BASH type shells, this is usually the $? variable
  2. Name of the last command that was run, or something meaningful to end-users

Checks the output of the last executed command for errors; prompts for a shell if the $DEBUG environment variable is set.

Example usage:

/bin/true
cmd_status $? "/bin/true"
/bin/false
cmd_status $? "/bin/false"
# running /bin/false should output an error message

want_shell ()

Arguments:

  1. None

Reads the $DEBUG environment variable to determine whether or not to prompt the user for a shell; if $DEBUG is not set, the user is not prompted. Your script should set $DEBUG to something prior to executing the want_shell function.

Example usage:

DEBUG=1
want_shell
# user is prompted for a shell

file_parse ()

Arguments:

  1. file to parse
  2. string to search for in that file

Reads the specified file, grepping for the specified string; returns a substring of the matched string containing all text after the first equals sign =. Sets the $PARSED environment variable with the string that was parsed out of the file.

Example usage:

echo "string=value" > /tmp/test.txt

# later on, somewhere in a shell script... 
file_parse /tmp/test.txt "string"
# $PARSED would then contain 'value'

get_pid ()

Arguments:

  1. The name of the binary that you want to retrieve the PID for

Returns the child PID as $CHILD_PID.

Example usage:

/bin/someprogram &
get_pid "someprogram"
echo $CHILD_PID # outputs the PID of 'someprogram'

write_child_pid ()

Arguments:

  1. the name of the child binary that was just executed (for creating the PID file name)
  2. the actual PID file of the child binary; in KSH/BASH scripts, this would be the $! variable

Writes the child PID into the file /var/run/${CHILD_BINARY}.pid. Meanted to be used with get_pid () above to write out PID files for processes that don't create their own.

Example usage:

/bin/someprogram &
get_pid "someprogram"
write_child_pid "someprogram" 
cat /var/run/someprogram.pid # outputs the PID of 'someprogram'

get_hostname ()

Arguments:

  1. None

Sets $HOSTNAME with the hostname of the machine, as obtained from the file /etc/hostname. Outputs an error if /etc/hostname does not exist.

get_kernel_version ()

Arguments:

  1. None

Sets environment variables for the kernel major, minor and patch version numbers as:

  • KERNEL_MAJOR
  • KERNEL_MINOR
  • KERNEL_PATCH_VERSION

Powered by Google Project Hosting