Gentoo Archives: gentoo-dev

From: Christopher Schwan <cschwan@××××××××××××××××××.de>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] RFC: sed script redundancy
Date: Sun, 29 May 2011 11:50:45
Message-Id: 201105291349.50497.cschwan@students.uni-mainz.de
In Reply to: Re: [gentoo-dev] RFC: sed script redundancy by Fabian Groffen
1 On Sunday 29 May 2011 13:00:32 Fabian Groffen wrote:
2 > On 29-05-2011 12:44:46 +0200, Christopher Schwan wrote:
3 > > Thank you for that script. I experimented a bit with it and have a number
4 > > of corrections and suggestions:
5 > >
6 > > - alias does not work because my_sed is not declared at this stage. I
7 > > removed the whole alias line because I want to selectively enable my_sed
8 > >
9 > > - oargs must be an array in order to make quoting work:
10 > > local oargs=( "${@}" )
11 > >
12 > > - In the ewarn line ${oargs} should be changed to ${nargs[@]} (!?)
13 > > - is it correct to treat -e and -f alike ? I am not sure about that,
14 > > because the latter expects a file
15 >
16 > Yes, because (also in your function) you always shift, and assume the
17 > next argument is there. Hence, you have two identical cases in your
18 > script now. I only distinguised between 1) being able to do something
19 > (-i) and 2) having a pattern to work with (-e/-f or first non-option
20 > argument as string pattern).
21
22 Ok sorry - I did not express myself clearly. Your script is replacing "-f"
23 with "-e" and I wasnt sure if this is correct. Buf of course, they can be
24 treated alike:
25
26 -e|--expression|-f|--file)
27 arg="$1"
28 shift
29 nargs+=( "${arg}" "$1" )
30 hade=yes
31 ;;
32
33 >
34 > > - If no "-e" is given, the first non-option argument is treated as the
35 > > sed- script-expression, therefore I added hade=yes in the if-branch
36 >
37 > That one was missing indeed. I just quickly wrote the proof of concept
38 >
39 > :)
40 > :
41 > > The new function now reads:
42 > [snip improved function]
43 >
44 > > As you can see, I added support for long-options. However, testing the
45 > > individual sed commands remains to be done. This could be especially
46 > > difficult if input is taken from stdin (e.g. in cat foo | sed
47 > > "s:a:b:g").
48 >
49 > You might be able to detect input is a pipe, and temporarily
50 > write the input to some file, then perform the sed without the -i
51 > requirement and remove the temp file after the real sed.
52
53 Good idea, thanks!
54
55 >
56 > > I tested my_sed within our sage ebuild[1]. This ebuild contains 39 sed
57 > > commands and I was able to spot one useless sed.
58 >
59 > Cool, nice to see you've made it into something useful!
60 >
61 > > [1] https://github.com/cschwan/sage-on-gentoo/blob/master/sci-
62 > > mathematics/sage/sage-4.7.ebuild