Blame Scripts/Functions/cli_getFilesList.sh

b7b189
#!/bin/bash
b7b189
#
b7b189
# cli_getFilesList.sh -- This function defines the list of FILES to
b7b189
# process.
b7b189
#
2d3646
# Copyright (C) 2009, 2010, 2011 The CentOS Project
fa95b1
#
fa95b1
# This program is free software; you can redistribute it and/or modify
fa95b1
# it under the terms of the GNU General Public License as published by
dcd347
# the Free Software Foundation; either version 2 of the License, or (at
dcd347
# your option) any later version.
fa95b1
#
74a058
# This program is distributed in the hope that it will be useful, but
74a058
# WITHOUT ANY WARRANTY; without even the implied warranty of
b7b189
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b7b189
# General Public License for more details.
b7b189
#
b7b189
# You should have received a copy of the GNU General Public License
b7b189
# along with this program; if not, write to the Free Software
dcd347
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7ac5a5
#
b7b189
# ----------------------------------------------------------------------
b7b189
# $Id$
b7b189
# ----------------------------------------------------------------------
b7b189
b7b189
function cli_getFilesList {
b7b189
384196
    # Define short options.
384196
    local ARGSS=''
8eeb3a
384196
    # Define long options.
6eba8c
    local ARGSL='pattern:,maxdepth:,type:,uid:'
384196
384196
    # Initialize arguments with an empty value and set it as local
384196
    # variable to this function scope.
384196
    local ARGUMENTS=''
384196
384196
    # Initialize pattern used to reduce the find output.
384196
    local PATTERN="$FLAG_FILTER"
384196
384196
    # Initialize options used with find command.
384196
    local OPTIONS=''
384196
384196
    # Redefine ARGUMENTS variable using current positional parameters. 
384196
    cli_doParseArgumentsReDef "$@"
384196
384196
    # Redefine ARGUMENTS variable using getopt output.
384196
    cli_doParseArguments
384196
384196
    # Redefine positional parameters using ARGUMENTS variable.
384196
    eval set -- "$ARGUMENTS"
384196
384196
    while true;do
384196
        case "$1" in
384196
384196
            --pattern )
384196
                PATTERN="$2"
384196
                shift 2
384196
                ;;
384196
384196
            --maxdepth )
384196
                OPTIONS="$OPTIONS -maxdepth $2"
384196
                shift 2
384196
                ;;
384196
b83a96
            --type )
b83a96
                OPTIONS="$OPTIONS -type $2"
b83a96
                shift 2
b83a96
                ;;
b83a96
6eba8c
            --uid )
6eba8c
                OPTIONS="$OPTIONS -uid $2"
6eba8c
                shift 2
6eba8c
                ;;
6eba8c
384196
            -- )
384196
                shift 1
384196
                break
384196
                ;;
384196
        esac
384196
    done
384196
384196
    # At this point all options arguments have been processed and
384196
    # removed from positional parameters. Only non-option arguments
384196
    # remain so we use them as source location for find command to
384196
    # look files for.
384196
    local LOCATIONS="$@"
384196
62fd79
    # Verify locations.
62fd79
    cli_checkFiles ${LOCATIONS}
f5c4be
384196
    # Redefine pattern as regular expression. When we use regular
f5c4be
    # expressions with find, regular expressions are evaluated against
f5c4be
    # the whole file path.  This way, when the regular expression is
4a9a32
    # specified, we need to build it in a way that matches the whole
f5c4be
    # path. Doing so, everytime we pass the `--filter' option in the
384196
    # command-line could be a tedious task.  Instead, in the sake of
f5c4be
    # reducing some typing, we prepare the regular expression here to
f5c4be
    # match the whole path using the regular expression provided by
385cc4
    # the user as pattern. Do not use LOCATION variable as part of
385cc4
    # regular expresion so it could be possible to use path expansion.
385cc4
    # Using path expansion reduce the amount of places to find out
385cc4
    # things and so the time required to finish the task.
384196
    PATTERN="^.+/${PATTERN}$"
8eeb3a
385cc4
    # Define list of files to process. At this point we cannot verify
385cc4
    # whether the LOCATION is a directory or a file since path
385cc4
    # expansion coul be introduced to it. The best we can do is
385cc4
    # verifying exit status and go on.
b83a96
    local FILES=$(find ${LOCATIONS} -regextype posix-egrep ${OPTIONS} -regex "${PATTERN}" | sort | uniq)
8eeb3a
d1225f
    # Output list of files to process.
385cc4
    if [[ $? -eq 0 ]];then
385cc4
        echo "$FILES"
385cc4
    fi
b7b189
b7b189
}