Difference between revisions of "Bash script template"

From Teknologisk videncenter
Jump to: navigation, search
m (Created page with "Below is a script template with options. <source lang=bash> #!/bin/bash # # FILNAME - SHORT DESCRIPTION # # (c) 2012 - DONALD DUCK <dd@disney.org> - all rights reserved # # THIS ...")
(No difference)

Revision as of 07:10, 2 May 2012

Below is a script template with options.

#!/bin/bash
#
# FILNAME - SHORT DESCRIPTION
#
# (c) 2012 - DONALD DUCK <dd@disney.org> - all rights reserved
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# default settings
COUNT=0
unset R # Default unset
N=1     # Default set
UNIT=""

usage() {
        echo "usage: $0 [-mkrh] [-c count -n] [-t text]"
        echo "  -m    m flag fx. report result in miles"
        echo "  -k    k flag fx. report result in km"
        echo "  -r    r flag"
        echo "  -c    c flag with value"
        echo "  -n    if c flag ...."
        echo "  -t    t flag with text"
        echo "  -h    help"
        echo ""
        echo "  Description line1"
    echo "      Description line1"
    exit 1
}

# collect options and override default

while getopts 'c:mkrr:n' CMDLINEARG
do
        case "${CMDLINEARG}" in
        c)
                COUNT=${OPTARG}
                ;;
        m|k)
                [ ! -z "${UNIT}" ] && echo '-m and -k are mutually exclusive. Select miles or km.'\
                        && usage
                UNIT="${CMDLINEARG}"
                ;;
        r)
                R=${CMDLINEARG}
                ;;
        n)
                N=${OPTARG}
                ;;
        h|*)
                usage
                ;;
        esac
done
echo -e "Settings"
echo -e "COUNT = $COUNT"
echo -e "UNIT  = $UNIT"
echo -e "R     = $R"
echo -e "N     = $N"
echo -e "Done..."