Blame Automation/Scripts/tcar_getConfigLines.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado eb3010
#   tcar_getConfigLines.sh -- This function standardizes the way
Alain Reguera Delgado 615395
#   configuration lines are retrieved form configuration files. As
Alain Reguera Delgado 615395
#   arguments, the configuration file absolute path, the configuration
Alain Reguera Delgado 615395
#   section name, and the configuration option name must be provided.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#   Written by: 
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 615395
#     Key fingerprint = D67D 0F82 4CBD 90BC 6421  DF28 7CCE 757C 17CA 3951
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 615395
# the Free Software Foundation; either version 2 of the License, or
Alain Reguera Delgado 615395
# (at your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 615395
Alain Reguera Delgado eb3010
function tcar_getConfigLines {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize absolute path to configuration file.
Alain Reguera Delgado 615395
    local CONFIGURATION_FILE="${1}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify that configuration file does exist.
Alain Reguera Delgado eb3010
    tcar_checkFiles -e ${CONFIGURATION_FILE}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize configuration section name where the variable value
Alain Reguera Delgado 8f60cb
    # we want to to retrieve is set in.
Alain Reguera Delgado 615395
    local CONFIGURATION_SECTION="${2}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Be sure the configuration section name has the correct format.
Alain Reguera Delgado 615395
    if [[ ! ${CONFIGURATION_SECTION} =~ '^[[:alnum:]._-]+$' ]];then
Alain Reguera Delgado eb3010
        tcar_printMessage "`gettext "The configuration section provided is incorrect."`" --as-error-line
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Initialize variable name we want to retrieve value from.
Alain Reguera Delgado 615395
    local CONFIGURATION_OPTION="${3}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify configuration variable name. When no variable name is
Alain Reguera Delgado 8f60cb
    # provided print all configuration lines that can be considered as
Alain Reguera Delgado 8f60cb
    # well-formed paths. Be sure configuration variable name starts
Alain Reguera Delgado 8f60cb
    # just at the beginning of the line.
Alain Reguera Delgado 615395
    if [[ ! ${CONFIGURATION_OPTION} =~ '^[[:alnum:]_./-]+$' ]];then
Alain Reguera Delgado 615395
        CONFIGURATION_OPTION='[[:alnum:]_./-]+[[:space:]]*='
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Retrieve configuration lines from configuration file.
Alain Reguera Delgado 615395
    local CONFIGURATION_LINES=$(cat ${CONFIGURATION_FILE} \
Alain Reguera Delgado 8f60cb
        | egrep -v '^#' \
Alain Reguera Delgado 8f60cb
        | egrep -v '^[[:space:]]*$' \
Alain Reguera Delgado 615395
        | sed -r -n "/^\[${CONFIGURATION_SECTION}\][[:space:]]*$/,/^\[/p" \
Alain Reguera Delgado 8f60cb
        | egrep -v '^\[' | sort | uniq \
Alain Reguera Delgado 615395
        | egrep "^${CONFIGURATION_OPTION}")
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Output value related to variable name.
Alain Reguera Delgado 615395
    echo "${CONFIGURATION_LINES}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}