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
#
e6bbbf
# Copyright (C) 2009-2013 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.
71ff20
    if [[ ! $CONFIG_SECTION =~ '^[[:alnum:]._-]+$' ]];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
123ee8
        CONFIG_OPTION='[[:alnum:]_./-]+[[:space:]]*='
878a2b
    fi
878a2b
dfc565
    # Retrieve configuration lines from configuration file.
878a2b
    local CONFIG_LINES=$(cat ${CONFIG_ABSPATH} \
878a2b
        | egrep -v '^#' \
878a2b
        | egrep -v '^[[:space:]]*$' \
71ff20
        | sed -r -n "/^\[${CONFIG_SECTION}\][[:space:]]*$/,/^\[/p" \
878a2b
        | egrep -v '^\[' | sort | uniq \
dfc565
        | egrep "^${CONFIG_OPTION}")
878a2b
878a2b
    # Output value related to variable name.
878a2b
    echo "$CONFIG_LINES"
878a2b
878a2b
}