1 |
Steve Long wrote: |
2 |
|
3 |
> Vaeth wrote: |
4 |
> |
5 |
> > let me remark that the more clever way to this is |
6 |
> > |
7 |
> > [ -n "${DOCS}" ] && eval "dodoc ${DOCS}" |
8 |
> > |
9 |
> eval is _not_ clever. Try: /msg greybot eval |
10 |
> ..or check http://wooledge.org:8000/BashFAQ/048 |
11 |
|
12 |
This is not at all related with my remark: |
13 |
We were speaking about the variable DOCS, which is supposed to be |
14 |
defined by a package author, not by an unreliable source. |
15 |
Of course, unreliable data here may allow execution of arbitrary code, |
16 |
but the package author can execute what he wants anyway. |
17 |
|
18 |
> > This way, people can simply quote as they like: |
19 |
> > |
20 |
> > DOCS="'filename with spaces' filename_without_space doc/*" |
21 |
> > |
22 |
> Yeuch. |
23 |
|
24 |
Well: DOCS=('filename with spaces' filename_without_space doc/*) |
25 |
I cannot see much difference: ( ) vs. " " would optically IMHO not be a |
26 |
reason to discuss, but the former works only in bash, the latter |
27 |
practically everyhwere (and so shell programmers should be used to the |
28 |
latter notation anyway). |
29 |
|
30 |
> > or also |
31 |
> > |
32 |
> > DOCS="just_one_filename_without_special_characters" |
33 |
> > |
34 |
> You don't need quotes there. |
35 |
|
36 |
This is true, but I wanted to show the way most people will use it. |
37 |
|
38 |
> > or also - when Push from /usr/bin/functions-eix.sh is used |
39 |
> > (which might be implemented simpler without using other functions): |
40 |
> > |
41 |
> > Push DOCS 'filename with spaces' filename_without_space "${S}"/doc/* |
42 |
> > |
43 |
> Or just do DOCS+=(foo/* someFile 'some other File') at any point. |
44 |
|
45 |
So the difference is saving two tokens. Is this worth to cement |
46 |
bash-dependency forever in many scripts? |
47 |
|
48 |
> BASH arrays will cope with *any* character apart from NUL, which isn't |
49 |
> allowed in filenames. Can you _guarantee_ the same? |
50 |
|
51 |
Yes, Push does _guarantee_ the same. It is actually rather simple to |
52 |
implement: It puts its argument in '...', separated by spaces, |
53 |
but replaces ' in the arguments before by '\'' (the last part is a bit |
54 |
tricky to do in POSIX [although not really hard - only in functions-eix.sh |
55 |
this is lengthy, because a more general replacement function is used |
56 |
there]. For the time being, I would not even argue against implementing |
57 |
Push in a sourced script in bash: This is only one place to change if one |
58 |
wants more compatibility later on). |
59 |
|
60 |
> For instance, what if some crazy designer puts a file called: |
61 |
> Vaeth's "Latest" Hits |
62 |
> ..in that doc dir |
63 |
|
64 |
no problem at all. |
65 |
|
66 |
> Ebuilds require BASH; get over it. |
67 |
|
68 |
My remark concerning arrays was meant to be general, not specific for |
69 |
ebuilds/portage only (although I couldn't find a passage in the bible |
70 |
where god claimed that ebuilds have to require bash. Actually, 99% of |
71 |
the ebuilds would not need bash, if they would be modified in a completely |
72 |
trivial ways (for the remaining 1% it would need a bit more work)). |
73 |
If one encourages people to write ebuilds compatible, maybe even for |
74 |
portage some day a change is realistic (although I am completely aware |
75 |
that this is not a reasonable project for the near future). |
76 |
|
77 |
> BASH is as portable as GNU make is, and you clearly have no issue |
78 |
> depending on that, and Python or C++. |
79 |
|
80 |
Do you know which shell might be preferrable in 5 years or 10 years? |
81 |
Bets are good that thos shell will at least support POSIX; |
82 |
bets are worse that this shell will support the bash-specific |
83 |
treatment of arrays. |
84 |
|
85 |
> BTW, POSIX sh doesn't need ${DOCS} or ${S} either, you're just wasting |
86 |
> characters. |
87 |
|
88 |
Yes, but that's the gentoo-recommended way to write variables - |
89 |
no need to change the style just for changing it. |
90 |
|
91 |
> > the array-less solution is also much simpler to |
92 |
> > implement, easy to understand from the source, and clearer in usage. |
93 |
> |
94 |
> Not to me it's not, it looks awful, to read and to type, as well as being |
95 |
> fragile. |
96 |
|
97 |
Yes, two symbols to type more is a nightmare :) |
98 |
"Fragile" is not the case as I showed above. |
99 |
|
100 |
> Furthermore you're bringing eval into the script new people are going to |
101 |
> look at to learn from (it's core functionality, fulfilling a basic task) |
102 |
|
103 |
So why should people learn bashisms instead of compatible shell |
104 |
programming? Eval is not bad in itself. It is only bad if used on |
105 |
uncontrolled data which I never suggested. (Actually, a warning in an |
106 |
portage manpage might be didactically more effective concerning the |
107 |
dangers of the uncontrolled usage of eval than many shell introductions.) |
108 |
|
109 |
> >> if isArr DOCS; then |
110 |
> >> ((${#DOCS[@]})) && dodoc "${DOCS[@]}" |
111 |
> >> else [[ $DOCS ]] && dodoc $DOCS |
112 |
> >> fi |
113 |
> > |
114 |
> > are just awful. |
115 |
> |
116 |
> Actually if you factor out that isArr is a utility function (exactly like |
117 |
> Push) that code is very easy to follow |
118 |
|
119 |
Maybe my explanation was unclear here: I am not speaking about the code. |
120 |
I am speaking about the way it behaves. |
121 |
DOCS='"a b"' -> two files `"a' and `b"' |
122 |
DOCS=('"a b"') -> one file `"a b"' |
123 |
this is just creating confusion by special cases. |
124 |
If you say instead the argument is eval'ed, everybody who knows any shell |
125 |
knows what is going on and that you have to quote correspondingly. |
126 |
And the case distinction is necessary, since for arrays you cannot |
127 |
shortcut (i.e. you can _never_ avoid the ( ) part) - for variables |
128 |
you can (as you mentioned, in most cases you can even avoid the " " part). |
129 |
|
130 |
> I'm willing to bet your sh scripts aren't really as portable as you think. |
131 |
> If you want to see how portable sh is done, read: |
132 |
> http://sources.redhat.com/autobook/autobook/autobook_210.html#SEC210 |
133 |
> (all of it) and then try to persuade us that we should be writing ebuilds |
134 |
> like that. |
135 |
|
136 |
This is an old rhetorical trick (I don't know its name in English): |
137 |
You impute that I claimed things which I never said - of course, then it |
138 |
is easy for you to prove that these things are wrong. |
139 |
I _never_ suggested to use code from stone-age for ebuilds (I did more |
140 |
for the eix scripts, and I think that I succeeded meanwhile for all |
141 |
architectures supported by gentoo, but I did never suggest this for |
142 |
everybody. BTW: Even for these architectures only very few differences |
143 |
from POSIX arose - these really old shells which do not have even |
144 |
functions or other odd bugs seem to have really extinct. But this is a |
145 |
different topic). |
146 |
|
147 |
However, I strongly suggest to avoid bashisms unless absolutely |
148 |
necessary and reasonable. There are scripts where this is reasonable, |
149 |
but far too many scripts which use it do not belong to this category. |
150 |
Using arrays to pass parameters is one of the cases of unnecessary usage |
151 |
(although this is not widely known - that's that main reason why I posted |
152 |
the remark). |