Subversion Repositories svn.Prod repos

Rev

Rev 33 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/env bash 
# =============================================================================
# Create
# name  : Andre Rikkert de Koe          date:   apr 2011
# descr : filefind
#         This is a demo of programming in the bash language.
#         Its part of a project to implement a program to create a directory
#         tree similar to the output of the Unix find program.
# EUID  : Any user who has read access to all the files being searched for.
# run   : interactive
#
# Changes
# name  :                               date:
# descr : short description
#
# =============================================================================

# =============================================================================
# Functions
# =============================================================================

Usage()
{
        mesg="$1"

        echo "Usage: $progname directory"
        echo $mesg
}

#
# we need local vars to keep the value when called recursively
#
traverse()
{
        local directory="$1"

        local curdir curdir2

        curdir=$(pwd)
        cd -- "$directory"
        curdir2=$(pwd)
        files=$(ls -a)
        for file in $files 
        do
                pfile=$curdir2/$file    # file including path
                if [ $file != ".." -a $file != "." ]
                then
                        if [ -d "$file" -a ! -L "$file" ]
                        then
                                echo $pfile
                                traverse "$file"
                        else
                                if [ -f $file -o -L $file ]
                                then
                                        echo "$pfile"
                                else
                                        echo "WARNING: Could not process file $pfile"
                                fi
                        fi

                fi      
        done
        cd -- "$curdir"
}

start_traverse()
{
        directory=$1

        echo $directory
        traverse $directory
}

# =============================================================================
# MAIN
# =============================================================================

progname=$(basename $0)
if [ $# -eq 1 ]
then
        directory=$1
        if [ -d $directory ]
        then 
                start_traverse $directory
        else
                Usage "ERROR: No a directory : $directory"
        fi
else
        Usage "ERROR: 1 argument expected"
fi