Blame Scripts/Bash/Functions/Commons/cli_getConfigLines.sh

878a2b
#!/bin/bash
878a2b
#
dfc565
# cli_getConfigLines.sh -- This function standardizes the way
dfc565
# configuration lines are retrieved form configuration files. As
dfc565
# arguments, the configuration file absolute path, the configuration
dfc565
# section name, and the configuration option name must be provided.
878a2b
#
03486a
# Copyright (C) 2009, 2010, 2011, 2012 The CentOS Project
878a2b
#
878a2b
# This program is free software; you can redistribute it and/or modify
878a2b
# it under the terms of the GNU General Public License as published by
878a2b
# the Free Software Foundation; either version 2 of the License, or (at
878a2b
# your option) any later version.
878a2b
#
878a2b
# This program is distributed in the hope that it will be useful, but
878a2b
# WITHOUT ANY WARRANTY; without even the implied warranty of
878a2b
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
878a2b
# General Public License for more details.
878a2b
#
878a2b
# You should have received a copy of the GNU General Public License
878a2b
# along with this program; if not, write to the Free Software
878a2b
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
878a2b
#
878a2b
# ----------------------------------------------------------------------
878a2b
# $Id$
878a2b
# ----------------------------------------------------------------------
878a2b
    
878a2b
function cli_getConfigLines {
878a2b
878a2b
    # Initialize absolute path to configuration file.
878a2b
    local CONFIG_ABSPATH="$1"
878a2b
33e41d
    # Verify that configuration file does exist.
33e41d
    cli_checkFiles -e ${CONFIG_ABSPATH}
878a2b
878a2b
    # Initialize configuration section name where the variable value
dfc565
    # we want to to retrieve is set in.
878a2b
    local CONFIG_SECTION="$2"
878a2b
dfc565
    # Be sure the configuration section name has the correct format.
dfc565
    if [[ ! $CONFIG_SECTION =~ '^[[:alpha:]]+$' ]];then
dfc565
        cli_printMessage "`gettext "The configuration section provided is incorrect."`" --as-error-line
dfc565
    fi
dfc565
dfc565
    # Initialize variable name we want to retrieve value from.
dfc565
    local CONFIG_OPTION="$3"
878a2b
878a2b
    # Verify configuration variable name. When no variable name is
dfc565
    # provided print all configuration lines that can be considered as
dfc565
    # well-formed paths. Be sure configuration variable name starts
dfc565
    # just at the beginning of the line.
dfc565
    if [[ ! $CONFIG_OPTION =~ '^[[:alnum:]_./-]+$' ]];then
dfc565
        CONFIG_OPTION='[[:alnum:]_./-]+='
878a2b
    fi
878a2b
dfc565
    # Retrieve configuration lines from configuration file.
878a2b
    local CONFIG_LINES=$(cat ${CONFIG_ABSPATH} \
878a2b
        | egrep -v '^#' \
878a2b
        | egrep -v '^[[:space:]]*$' \
878a2b
        | sed -r 's![[:space:]]*!!g' \
878a2b
        | sed -r -n "/^\[${CONFIG_SECTION}\]$/,/^\[/p" \
878a2b
        | egrep -v '^\[' | sort | uniq \
dfc565
        | egrep "^${CONFIG_OPTION}")
878a2b
878a2b
    # Output value related to variable name.
878a2b
    echo "$CONFIG_LINES"
878a2b
878a2b
}