1 |
On Thu, May 19, 2016 at 10:50:48PM +0100, Amadeusz Żołnowski wrote: |
2 |
> "Robin H. Johnson" <robbat2@g.o> writes: |
3 |
> > 1. |
4 |
> > If your AWK command has an error, then this clobbers the file, a |
5 |
> > better variant, with die, would be: |
6 |
> > awk "$@" "$f" >"${tmpf}" || die "awk failed..." |
7 |
> > cp -f "${tmpf}" "$f" || die "copy back failed" |
8 |
> |
9 |
> Thanks! On the one hand it's a simple function, and on the other hand |
10 |
> there are several things which can go wrong. I will submit corrected |
11 |
> version with unit test. |
12 |
> |
13 |
> Why '-f' is required? |
14 |
Corner cases w/ copying and bad ebuild usage (specifically running |
15 |
ebuild as two different non-root users during development). Mostly |
16 |
habit. |
17 |
|
18 |
Another thought I had, is that if you're using eawk on something in |
19 |
$ROOT, you're also going to clobber ownership, permissions and extended |
20 |
attributes. |
21 |
|
22 |
> >> # @USAGE: <file> <args> |
23 |
> > This calling format is also a problem if they intended to pass multiple |
24 |
> > files to the command, or if they do so accidentally. |
25 |
> > |
26 |
> > Eg, an unprotected glob in the first argument, eg: |
27 |
> > eawk foo* '/foobar/{$1="x"; print $0}' |
28 |
> > |
29 |
> > If foo1 and foo2 existed, this would be considered as: |
30 |
> > awk foo2 /foobar/{$1="x"; print $0}' foo1 |
31 |
> > |
32 |
> > which really will NOT have the desired effect. |
33 |
> |
34 |
> I am aware of this. I considered before: |
35 |
> |
36 |
> a) eawk files... -- awk_args... |
37 |
> b) eawk awk_args -- pattern files... |
38 |
> |
39 |
> making '--' required, but that just complicates usage and function |
40 |
> itself. And anyway isn't it true for any other command with careless |
41 |
> glob use? |
42 |
> |
43 |
> I have documented that function takes a file, not files. The effect of |
44 |
> misuse eawk would be awk error. Although maybe it would be useful to |
45 |
> support multiple files? Then I'd go for variant (b) probably. |
46 |
If we declare the calling format in BSD style: |
47 |
<args> <files> |
48 |
|
49 |
Then this is negated, but means you need to be able to find the first |
50 |
file in the list, which requires parsing the options correctly. |
51 |
|
52 |
Another variation that you haven't considered above is passing multiple |
53 |
programs to awk via -e or -f options, in which case there is no |
54 |
non-option program argument. |
55 |
|
56 |
I agree generally that this is a useful idea, but there's a lot of |
57 |
caveats and corner-cases :-(. |
58 |
|
59 |
-- |
60 |
Robin Hugh Johnson |
61 |
Gentoo Linux: Dev, Infra Lead, Foundation Trustee & Treasurer |
62 |
E-Mail : robbat2@g.o |
63 |
GnuPG FP : 11ACBA4F 4778E3F6 E4EDF38E B27B944E 34884E85 |
64 |
GnuPG FP : 7D0B3CEB E9B85B1F 825BCECF EE05E6F6 A48F6136 |