Loading...

Create help output with echo in shell scripts

:heavy_exclamation_mark: This post is older than a year. Consider some information might not be accurate anymore. :heavy_exclamation_mark:

Writing bash (shell) scripts is sometimes necessary to automate little task and ease the maintenance. Providing a good help is also must, for the others admins, that have to use it. This post illustrates a small example.

Following program checks for three essential commands. If they aren’t available (installed) it displays a message and shows all options in an example function. echo interprets backslash characters with the -e option. Below example prints the information in a newline \n.

#!/bin/bash
# Usage information
displayhelp () {
    echo -e "\nUsage: `basename $0` [options] input_files_or_directories ...\n"
    echo -e "OPTIONS:\n"
    echo " -h help ;)"
    echo " -d target directory (directory structure of input_directory is preserved)"
    echo -e " -r target bitrate"
    exit 0
}
# Required programs
OGGINFO=`which ogginfo`
OGGDEC=`which oggdec`
LAME=`which lame`
if [ "$OGGINFO"="" -o "$OGGDEC"="" -o "$LAME"="" ]; then
    echo -e "\nERROR: ogginfo, oggdec and lame are required!\n"
    displayhelp
    exit 0
fi

The output

root@pelion:~# ./help-demo.sh
ERROR: ogginfo, oggdec and lame are required!
Usage: help-demo.sh [options] input_files_or_directories ...
OPTIONS:
 -h help ;)
 -d target directory (directory structure of input_directory is preserved)
 -r target bitrate