Subversion Repositories svn.Prod repos

Rev

Rev 4 | Rev 37 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4 Rev 33
1
#!/usr/bin/env bash 
1
#!/usr/bin/env bash 
2
# =============================================================================
2
# =============================================================================
3
# Create
3
# Create
4
# name  : Andre Rikkert de Koe          date:   apr 2011
4
# name  : Andre Rikkert de Koe          date:   apr 2011
5
# descr : filefind
5
# descr : filefind
6
#         This is a demo of programming in the bash language.
6
#         This is a demo of programming in the bash language.
7
#         Its part of a project to implement a program to create a directory
7
#         Its part of a project to implement a program to create a directory
8
#         tree similar to the output of the Unix find program.
8
#         tree similar to the output of the Unix find program.
9
# EUID  : Any user who has read access to all the files being searched for.
9
# EUID  : Any user who has read access to all the files being searched for.
10
# run   : interactive
10
# run   : interactive
11
#
11
#
12
# Changes
12
# Changes
13
# name  :                               date:
13
# name  :                               date:
14
# descr : short description
14
# descr : short description
15
#
15
#
16
# =============================================================================
16
# =============================================================================
17
 
17
 
18
# =============================================================================
18
# =============================================================================
19
# Functions
19
# Functions
20
# =============================================================================
20
# =============================================================================
21
 
21
 
22
Usage()
22
Usage()
23
{
23
{
24
	mesg="$1"
24
	mesg="$1"
25
 
25
 
26
	echo "Usage : $progname directory"
26
	echo "Usage : $progname directory"
27
	echo $mesg
27
	echo $mesg
28
}
28
}
29
 
29
 
30
#
30
#
31
# we need local vars to keep the value when called recursively
31
# we need local vars to keep the value when called recursively
32
#
32
#
33
traverse()
33
traverse()
34
{
34
{
35
	local directory="$1"
35
	local directory="$1"
36
 
36
 
37
	local curdir curdir2
37
	local curdir curdir2
38
 
38
 
39
	curdir=$(pwd)
39
	curdir=$(pwd)
40
	cd -- "$directory"
40
	cd -- "$directory"
41
	curdir2=$(pwd)
41
	curdir2=$(pwd)
42
	files=$(ls)
42
	files=$(ls -a)
43
	for file in $files 
43
	for file in $files 
44
	do
44
	do
45
		pfile=$curdir2/$file	# file including path
45
		pfile=$curdir2/$file	# file including path
46
		if [ -d "$file" -a ! -L "$file" ]
46
		if [ $file != ".." -a $file != "." ]
47
		then
47
		then
48
			echo $pfile
-
 
49
			traverse "$file"
-
 
50
		else
-
 
51
			if [ -f $file -o -L $file ]
48
			if [ -d "$file" -a ! -L "$file" ]
52
			then
49
			then
53
				echo "$pfile"
50
				echo $pfile
-
 
51
				traverse "$file"
54
			else
52
			else
-
 
53
				if [ -f $file -o -L $file ]
-
 
54
				then
-
 
55
					echo "$pfile"
-
 
56
				else
55
				echo "WARNING: Could not process file $pfile"
57
					echo "WARNING: Could not process file $pfile"
-
 
58
				fi
56
			fi
59
			fi
-
 
60
 
57
		fi	
61
		fi	
58
	done
62
	done
59
	cd -- "$curdir"
63
	cd -- "$curdir"
60
}
64
}
61
 
65
 
62
start_traverse()
66
start_traverse()
63
{
67
{
64
	directory=$1
68
	directory=$1
65
 
69
 
66
	echo $directory
70
	echo $directory
67
	traverse $directory
71
	traverse $directory
68
}
72
}
69
 
73
 
70
# =============================================================================
74
# =============================================================================
71
# MAIN
75
# MAIN
72
# =============================================================================
76
# =============================================================================
73
 
77
 
74
progname=$(basename $0)
78
progname=$(basename $0)
75
if [ $# -eq 1 ]
79
if [ $# -eq 1 ]
76
then
80
then
77
	directory=$1
81
	directory=$1
78
	if [ -d $directory ]
82
	if [ -d $directory ]
79
	then 
83
	then 
80
		start_traverse $directory
84
		start_traverse $directory
81
	else
85
	else
82
		Usage "ERROR: No directory $directory found"
86
		Usage "ERROR: No directory $directory found"
83
	fi
87
	fi
84
else
88
else
85
	Usage "ERRROR: 1 argument expected"
89
	Usage "ERRROR: 1 argument expected"
86
fi
90
fi