Rev 37 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 37 | Rev 43 | ||
|---|---|---|---|
| 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 -a) |
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 [ $file != ".." -a $file != "." ] |
46 | if [ $file != ".." -a $file != "." ] |
| 47 | then |
47 | then |
| 48 | if [ -d "$file" -a ! -L "$file" ] |
48 | if [ -d "$file" -a ! -L "$file" ] |
| 49 | then |
49 | then |
| 50 | echo $pfile |
50 | echo $pfile |
| 51 | traverse "$file" |
51 | traverse "$file" |
| 52 | else |
52 | else |
| 53 | if [ -f $file -o -L $file ] |
53 | if [ -f $file -o -L $file ] |
| 54 | then |
54 | then |
| 55 | echo "$pfile" |
55 | echo "$pfile" |
| 56 | else |
56 | else |
| 57 | echo "WARNING: Could not process file $pfile" |
57 | echo "WARNING: Could not process file $pfile" |
| 58 | fi |
58 | fi |
| 59 | fi |
59 | fi |
| 60 | 60 | ||
| 61 | fi |
61 | fi |
| 62 | done |
62 | done |
| 63 | cd -- "$curdir" |
63 | cd -- "$curdir" |
| 64 | } |
64 | } |
| 65 | 65 | ||
| 66 | start_traverse() |
66 | start_traverse() |
| 67 | { |
67 | { |
| 68 | directory=$1 |
68 | directory=$1 |
| 69 | 69 | ||
| 70 | echo $directory |
70 | echo $directory |
| 71 | traverse $directory |
71 | traverse $directory |
| 72 | } |
72 | } |
| 73 | 73 | ||
| 74 | # ============================================================================= |
74 | # ============================================================================= |
| 75 | # MAIN |
75 | # MAIN |
| 76 | # ============================================================================= |
76 | # ============================================================================= |
| 77 | 77 | ||
| 78 | progname=$(basename $0) |
78 | progname=$(basename $0) |
| 79 | if [ $# -eq 1 ] |
79 | if [ $# -eq 1 ] |
| 80 | then |
80 | then |
| 81 | directory=$1 |
81 | directory=$1 |
| 82 | if [ -d $directory ] |
82 | if [ -d $directory ] |
| 83 | then |
83 | then |
| 84 | start_traverse $directory |
84 | start_traverse $directory |
| 85 | else |
85 | else |
| 86 | Usage "ERROR: No a directory : $directory" |
86 | Usage "ERROR: No a directory : $directory" |
| 87 | fi |
87 | fi |
| 88 | else |
88 | else |
| 89 | Usage "ERROR: 1 argument expected" |
89 | Usage "ERROR: 1 argument expected" |
| 90 | fi |
90 | fi |