]> git.ipfire.org Git - thirdparty/bash.git/blame - examples/functions/xfind.bash
Bash-4.2 patch 45
[thirdparty/bash.git] / examples / functions / xfind.bash
CommitLineData
bb70624e
JA
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
7
8# $1 = dirname, $2 = pattern, optional $3 = action
9xfind()
10{
11 local x
12 local dir="$1"
13
14 # descend into specified directory
15
16 builtin cd -L "$1" || {
17 echo "${FUNCNAME}: cannot change dir to $1" >&2
18 return 1
19 }
20
21 #
22 # default action is to print the filename
23 #
24 if [ -n "$3" ]; then
25 action="$3"
26 else
27 action='printf -- "%s\n"'
28 fi
29
30 # process ordinary files that match pattern
31
32 for x in $2 ; do
33 if [ -f "$x" ] ; then
34 eval "$action" "$x"
35 fi
36 done
37
38 # now descend into subdirectories, avoiding symbolic links
39 # and directories that start with a period.
40
41 for x in * ; do
b80f6443 42 if [ -d "$x" ] && [ ! -L "$x" ] ; then
bb70624e
JA
43 $FUNCNAME "$x" "$2" "$action"
44 fi
45 done
46
47 # finally, pop back up
48
49 builtin cd -L ..
50}
51
52#xfind "$@"