1 |
On 21-05-2011 19:34:34 +0200, Jeroen Roovers wrote: |
2 |
> On Fri, 20 May 2011 17:56:00 +0200 |
3 |
> Fabian Groffen <grobian@g.o> wrote: |
4 |
> |
5 |
> > sed -e "<pattern>" "${file}" | diff "${file}" - |
6 |
> > |
7 |
> > followed by the actual sed -i -e ... |
8 |
> > |
9 |
> > This way I didn't need to write an intermediate file. |
10 |
> |
11 |
> The problem there is that sed might be called just once on any one file, |
12 |
> but in the tree it is often invoked with multiple scripts, so this |
13 |
> simple implementation lacks a way to evaluate which sed scripts are |
14 |
> useful. |
15 |
> |
16 |
> Also, how do I ensure the sed replacement works only on invocations |
17 |
> inside the ebuild, and not, say, in portage's internals? |
18 |
|
19 |
(not tested, but as proof of concept) |
20 |
|
21 |
alias sed my_sed |
22 |
my_sed() { |
23 |
local oargs="${@}" |
24 |
local arg |
25 |
local nargs=() |
26 |
local hadi= |
27 |
local hade= |
28 |
while [[ -n $1 ]] ; do |
29 |
case "$1" in |
30 |
-i) |
31 |
# ignore this flag |
32 |
hadi=yes |
33 |
;; |
34 |
-e|-f) |
35 |
shift |
36 |
nargs+=( "-e$1" ) |
37 |
hade=yes |
38 |
;; |
39 |
-*) |
40 |
nargs+=( "$1" ) |
41 |
hade=yes |
42 |
;; |
43 |
*) |
44 |
if [[ -z ${hade} ]] ; then |
45 |
nargs+=( "$1" ) |
46 |
elif [[ -z ${hadi} ]] ; then |
47 |
# there is no inline replacing, not much we can do |
48 |
break |
49 |
else |
50 |
sed "${nargs[@]}" "$1" | diff -q "$1" - > /dev/null \ |
51 |
&& ewarn "sed ${oargs} has no effect on $1" |
52 |
fi |
53 |
;; |
54 |
esac |
55 |
shift |
56 |
done |
57 |
|
58 |
\sed "${oargs}" |
59 |
} |
60 |
|
61 |
|
62 |
-- |
63 |
Fabian Groffen |
64 |
Gentoo on a different level |