Rev 3 | Rev 33 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 3 | ls | 1 | #!/usr/bin/env bash |
| 4 | ls | 2 | # ============================================================================= |
| 3 | # Create |
||
| 4 | # name : Andre Rikkert de Koe date: apr 2011 |
||
| 5 | # descr : filefind |
||
| 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 |
||
| 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. |
||
| 10 | # run : interactive |
||
| 11 | # |
||
| 12 | # Changes |
||
| 13 | # name : date: |
||
| 14 | # descr : short description |
||
| 15 | # |
||
| 16 | # ============================================================================= |
||
| 3 | ls | 17 | |
| 18 | # ============================================================================= |
||
| 19 | # Functions |
||
| 20 | # ============================================================================= |
||
| 21 | |||
| 22 | Usage() |
||
| 23 | { |
||
| 24 | mesg="$1" |
||
| 25 | |||
| 26 | echo "Usage : $progname directory" |
||
| 27 | echo $mesg |
||
| 28 | } |
||
| 29 | |||
| 30 | # |
||
| 31 | # we need local vars to keep the value when called recursively |
||
| 32 | # |
||
| 33 | traverse() |
||
| 34 | { |
||
| 35 | local directory="$1" |
||
| 36 | |||
| 37 | local curdir curdir2 |
||
| 38 | |||
| 39 | curdir=$(pwd) |
||
| 40 | cd -- "$directory" |
||
| 41 | curdir2=$(pwd) |
||
| 42 | files=$(ls) |
||
| 43 | for file in $files |
||
| 44 | do |
||
| 45 | pfile=$curdir2/$file # file including path |
||
| 46 | if [ -d "$file" -a ! -L "$file" ] |
||
| 47 | then |
||
| 48 | echo $pfile |
||
| 49 | traverse "$file" |
||
| 50 | else |
||
| 51 | if [ -f $file -o -L $file ] |
||
| 52 | then |
||
| 53 | echo "$pfile" |
||
| 54 | else |
||
| 55 | echo "WARNING: Could not process file $pfile" |
||
| 56 | fi |
||
| 57 | fi |
||
| 58 | done |
||
| 59 | cd -- "$curdir" |
||
| 60 | } |
||
| 61 | |||
| 62 | start_traverse() |
||
| 63 | { |
||
| 64 | directory=$1 |
||
| 65 | |||
| 66 | echo $directory |
||
| 67 | traverse $directory |
||
| 68 | } |
||
| 69 | |||
| 70 | # ============================================================================= |
||
| 71 | # MAIN |
||
| 72 | # ============================================================================= |
||
| 73 | |||
| 74 | progname=$(basename $0) |
||
| 75 | if [ $# -eq 1 ] |
||
| 76 | then |
||
| 77 | directory=$1 |
||
| 78 | if [ -d $directory ] |
||
| 79 | then |
||
| 80 | start_traverse $directory |
||
| 81 | else |
||
| 82 | Usage "ERROR: No directory $directory found" |
||
| 83 | fi |
||
| 84 | else |
||
| 85 | Usage "ERRROR: 1 argument expected" |
||
| 86 | fi |