Blame Scripts/Bash/Functions/cli_checkFiles.sh

4c79b5
#!/bin/bash
4c79b5
#
4c79b5
# cli_checkFiles.sh -- This function checks file(s) existence.
4c79b5
#
4c79b5
# Copyright (C) 2009-2010 Alain Reguera Delgado
4c79b5
# 
4c79b5
# This program is free software; you can redistribute it and/or
4c79b5
# modify it under the terms of the GNU General Public License as
4c79b5
# published by the Free Software Foundation; either version 2 of the
4c79b5
# License, or (at your option) any later version.
4c79b5
# 
4c79b5
# This program is distributed in the hope that it will be useful, but
4c79b5
# WITHOUT ANY WARRANTY; without even the implied warranty of
4c79b5
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c79b5
# General Public License for more details.
4c79b5
#
4c79b5
# You should have received a copy of the GNU General Public License
4c79b5
# along with this program; if not, write to the Free Software
4c79b5
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
4c79b5
# USA.
4c79b5
# 
4c79b5
# ----------------------------------------------------------------------
4c79b5
# $Id: cli_checkFiles.sh 98 2010-09-19 16:01:53Z al $
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function cli_checkFiles {
4c79b5
4c79b5
    # Define variables as local to avoid conflicts outside.
4c79b5
    local FILE=''
4c79b5
    local FILES="$1"
4c79b5
    local TYPE="$2"
4c79b5
    local ACTION="$3"
4c79b5
    local MESSAGE=''
4c79b5
4c79b5
    # Check amount of paramaters passed. At least one argument should
4c79b5
    # be passed.
4c79b5
    if [[ $# -lt 1 ]];then
4c79b5
        cli_printMessage "`gettext "cli_checkFiles: At least one argument should be passed."`"
1f1b3c
        cli_printMessage "$(caller)" "AsToKnowMoreLine"
4c79b5
    fi
4c79b5
1f1b3c
    # Define default action to take, if it is not specified.
4c79b5
    if [[ ! "$3" ]] || [[ "$3" == '' ]];then
4c79b5
        ACTION=`gettext "Checking"`
4c79b5
    fi
4c79b5
4c79b5
    for FILE in $FILES;do
4c79b5
      
4c79b5
        case $TYPE in
4c79b5
4c79b5
            d | directory )
4c79b5
                # File exists and is a directory
4c79b5
                if [[ ! -d $FILE ]];then
4c79b5
                    MESSAGE="The directory \"$FILE\" doesn't exist."
4c79b5
                fi
4c79b5
                ;;
4c79b5
4c79b5
            f | regular-file )
4c79b5
                # File exists and is a regular file.
4c79b5
                if [[ ! -f $FILE ]];then
4c79b5
                    MESSAGE="The file \"$FILE\" is not a regular file."
4c79b5
                fi
4c79b5
                ;;
4c79b5
4c79b5
            h | symbolic-link )
4c79b5
                # File exists and is a symbolic link.
4c79b5
                if [[ ! -h $FILE ]];then
4c79b5
                    MESSAGE="The file \"$FILE\" is not a symbolic link."
4c79b5
                fi
4c79b5
                ;;
4c79b5
4c79b5
            fh )
4c79b5
                # To exist, file should be a regular file or a symbolic link.
4c79b5
                if [[ ! -f $FILE ]];then
4c79b5
                    if [[ ! -h $FILE ]];then
4c79b5
                        MESSAGE="The file \"$FILE\" doesn't exist."
4c79b5
                    fi
4c79b5
                fi
4c79b5
                ;;
4c79b5
4c79b5
            * )
4c79b5
                # File exists.
4c79b5
                if [[ ! -a $FILE ]];then
4c79b5
                    MESSAGE="The file \"$FILE\" doesn't exist."
4c79b5
                fi
4c79b5
4c79b5
        esac
4c79b5
4c79b5
        # If there is some message print it and continue with the next
4c79b5
        # FILE in the loop (2). We wouldn't continue if a file is
4c79b5
        # missing.
1f1b3c
        if [[ "$MESSAGE" == '' ]];then
4c79b5
            cli_printMessage "${ACTION}: $FILE"
4c79b5
        else
4c79b5
            cli_printMessage "${ACTION}: $MESSAGE"
4c79b5
            continue 2;
4c79b5
        fi
4c79b5
4c79b5
   done
4c79b5
4c79b5
}