Gentoo Archives: gentoo-dev

From: Steven J Long <slong@××××××××××××××××××.uk>
To: gentoo-dev@l.g.o
Subject: [gentoo-dev] Re: [PATCH eutils] Move remove_libtool_files() from autotools-utils for wider use.
Date: Tue, 29 May 2012 13:45:46
Message-Id: jq2jrp$tc9$1@dough.gmane.org
In Reply to: [gentoo-dev] [PATCH eutils] Move remove_libtool_files() from autotools-utils for wider use. by "Michał Górny"
1 Michał Górny wrote:
2
3 > + find "${D}" -type f -name '*.la' -print0 | while read -r -d '' f;
4 ..
5 > + rm -f "${f}" || die
6 ..
7 > + done
8
9 Don't pipe to read like that; it means the final command is in a subshell
10 and "die is /not/ guaranteed to work correctly if called from a subshell
11 environment."[1]
12
13 More seriously, the script doesn't actually get the correct filenames,
14 despite being written to handle any filename.
15 eg:
16 $ touch $' foo bar \n\t '
17 $ while read -r -d '' f; do echo "'$f'"; done < <(find . -type f -print0)
18 './ foo bar'
19
20 You do it like this:
21
22 while read -rd ''; do
23 f=$REPLY;
24 ..
25 done < <(find "$D" -type f -name '*.la' -print0)
26
27 eg:
28 $ while read -rd ''; do f=$REPLY; echo "'$f'"; done < <(find . -type f -
29 print0)
30 './ foo bar
31 '
32
33 Or use: while IFS= read -rd '' f; do .. if you prefer.
34 See: help read # in a terminal.
35
36 It's called 'Process Substitution' if anyone wants to read about it in
37 man bash. The classic example with find is to get the list in an array:
38 arr=()
39 while read -rd ''; do
40 arr+=("$REPLY")
41 done < <(find "$dir" -type f .. -print0)
42
43 (perhaps conditionally though that's usually better done within find
44 which can later be handled on a per-file basis, or passed to:
45 foo "${arr[@]}"
46
47 ..or if you just want to know whether there is a matching file:
48 if read -rd '' < <(find . -type f -print0); then
49 something matched
50 else nothing did
51 fi
52
53 They're both things I came up with a few years ago when I was learning
54 from #bash, which you are in dire need of, based on reading git-2.eclass.
55
56 [1] http://dev.gentoo.org/~ulm/pms/head/pms.html#x1-12600011.3.3
57 (11.3.3.6)
58 --
59 #friendly-coders -- We're friendly, but we're not /that/ friendly ;-)

Replies