Revision 21276,
1.7 KB
checked in by zacheiss, 20 years ago
(diff) |
This commit was generated by cvs2svn to compensate for changes in r21275,
which included commits to RCS files with non-trunk default branches.
|
Line | |
---|
1 | #!/bin/bash |
---|
2 | |
---|
3 | #From: kaz@ashi.footprints.net (Kaz Kylheku) |
---|
4 | #Newsgroups: comp.os.linux.misc |
---|
5 | #Subject: Re: bash question: subdirectories |
---|
6 | #Message-ID: <slrn8a0gu9.v5n.kaz@ashi.FootPrints.net> |
---|
7 | #Date: Tue, 08 Feb 2000 16:24:35 GMT |
---|
8 | |
---|
9 | #Actually it can be made to. That is to say, it is possible to code a recursive |
---|
10 | #descender function in the bash language. Here is an example. |
---|
11 | # |
---|
12 | #What is nice about this is that you can embed the function into your shell |
---|
13 | #script. The function changes the current working directory as it descends. |
---|
14 | #So it can handle arbitrarily deep paths. Whereas paths generated by the |
---|
15 | #find command can cause a problem when they get too long; the kernel has a |
---|
16 | #hard limit on the length of the string passed to the open() and other |
---|
17 | #system calls. |
---|
18 | |
---|
19 | #There are races; what if the directory tree is blown away during the traversal? |
---|
20 | #The function won't be able to crawl back up using the .. link and will just |
---|
21 | #bail. |
---|
22 | |
---|
23 | # Recursive Directory Traverser |
---|
24 | # Author: Kaz Kylheku |
---|
25 | # Date: Feb 27, 1999 |
---|
26 | # Copyright 1999 |
---|
27 | |
---|
28 | # Function parameter usage: |
---|
29 | # $1 directory to search |
---|
30 | # $2 pattern to search for |
---|
31 | # $3 command to execute |
---|
32 | # $4 secret argument for passing down path |
---|
33 | |
---|
34 | function recurse |
---|
35 | { |
---|
36 | local file |
---|
37 | local path |
---|
38 | |
---|
39 | if [ "$4" = "" ] ; then |
---|
40 | path="${1%/}/" |
---|
41 | else |
---|
42 | path="$4$1/" |
---|
43 | fi |
---|
44 | |
---|
45 | if cd "$1" ; then |
---|
46 | for file in $2; do |
---|
47 | if [ -f "$file" ] || [ -d "$file" ]; then |
---|
48 | eval "$3" |
---|
49 | fi |
---|
50 | done |
---|
51 | for file in .* * ; do |
---|
52 | if [ "$file" = "." ] || [ "$file" = ".." ] ; then |
---|
53 | continue |
---|
54 | fi |
---|
55 | if [ -d "$file" ] && [ ! -L "$file" ]; then |
---|
56 | recurse "$file" "$2" "$3" "$path" |
---|
57 | fi |
---|
58 | done |
---|
59 | cd .. |
---|
60 | fi |
---|
61 | } |
---|
62 | |
---|
63 | recurse "$1" "$2" 'echo "$path$file"' |
---|
Note: See
TracBrowser
for help on using the repository browser.