Blame Scripts/Functions/cli_getConfigLines.sh

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