Patch-ID: bash40-021
[bash.git] / examples / functions / xfind.bash
blob6d29038174dfd93f5bb0b53a102c7add6462732b
1 #! /bin/bash
2 #From: kaz@cafe.net (Kaz Kylheku)
3 #Newsgroups: comp.unix.shell
4 #Subject: Why not roll your own @#$% find! (was: splitting directory off from filename)
5 #Message-ID: <6n1117$tp1@espresso.cafe.net>
6 #Date: Fri, 26 Jun 1998 20:47:34 GMT
8 # $1 = dirname, $2 = pattern, optional $3 = action
9 xfind()
11 local x
12 local dir="$1"
14 # descend into specified directory
16 builtin cd -L "$1" || {
17 echo "${FUNCNAME}: cannot change dir to $1" >&2
18 return 1
22 # default action is to print the filename
24 if [ -n "$3" ]; then
25 action="$3"
26 else
27 action='printf -- "%s\n"'
30 # process ordinary files that match pattern
32 for x in $2 ; do
33 if [ -f "$x" ] ; then
34 eval "$action" "$x"
36 done
38 # now descend into subdirectories, avoiding symbolic links
39 # and directories that start with a period.
41 for x in * ; do
42 if [ -d "$x" ] && [ ! -L "$x" ] ; then
43 $FUNCNAME "$x" "$2" "$action"
45 done
47 # finally, pop back up
49 builtin cd -L ..
52 #xfind "$@"