1 |
Alistair Bush wrote: |
2 |
> Im sure Elvanor can't wait for you constructive feedback on his eclass |
3 |
> and depending on your feedback the eclass will enter the tree this |
4 |
> weekend. |
5 |
> |
6 |
A couple of very minor performance points, which I think are more |
7 |
significant in eclasses. Firstly the basename thing Donnie pointed out |
8 |
before: |
9 |
> > local feedfile=$(basename "${src}") |
10 |
> You could do this in pure bash, although it doesn't really matter: |
11 |
> local feedfile=${src##*/} |
12 |
|
13 |
[[ ${#} -lt 4 ]] && die "At least four arguments needed" |
14 |
Arithmetic context is quicker for this: |
15 |
(($#<4)) && die "At least four arguments needed" |
16 |
although in this case, _java-pkg_osgijar(), it looks it requires exactly 4: |
17 |
(($#==4)) || die 'Four arguments needed' |
18 |
|
19 |
You have a couple of functions that take, say, 4 or 5 arguments. It would be |
20 |
more robust to use a case, eg: |
21 |
case $# in |
22 |
5)..;; |
23 |
4)..;; |
24 |
*) die "Incorrect use of $FUNCNAME";; |
25 |
esac |
26 |
..than if (($#>4)); then ..; else .. ;fi |
27 |
|
28 |
_java-pkg_osgi-plugin ${4} # this should be quoted (_java-pkg_osgijar) |
29 |
|
30 |
With regard to: |
31 |
debug-print-function ${FUNCNAME} $* |
32 |
if you want debug-print-function to get the arguments correctly, use "$@" |
33 |
not $* (cf 'Special Parameters' in man bash for a proper explanation: this |
34 |
applies to all arrays. http://wooledge.org/mywiki/BashFAQ/073 shows how to |
35 |
manipulate all members of an array, eg to add a prefix.) |
36 |
|
37 |
This use of counter in _java-pkg_osgijar-fromfile is odd: |
38 |
while [[ -n "${1}" ]]; do |
39 |
# while [[ $1 ]] or while (($#)) also work |
40 |
if [[ "${1}" == "--noversion" ]]; then |
41 |
noversion=1 |
42 |
else |
43 |
arguments[${counter}]="${1}" |
44 |
((++counter)) |
45 |
fi |
46 |
shift 1 # just shift? |
47 |
done |
48 |
|
49 |
((${#arguments[@]} < 3)) && die "At least three arguments (not |
50 |
counting --noversion) are needed for java-pkg_osgijar-fromfile()" |
51 |
|
52 |
You can either just add to the array with: arguments+=("$1") |
53 |
or add using the counter: arguments[counter++]="$1" |
54 |
and then check: ((counter < 3)) && die .. |
55 |
|
56 |
Arithmetic context[1] applies in array indexes, so you don't need to use a $ |
57 |
for variables and you can post/pre-incr etc there. |
58 |
Yuu can also use it for C-style flags, with 0 as false and non-zero true: |
59 |
if [[ "${noversion}" == 1 ]]; |
60 |
can be |
61 |
if ((noversion)); |
62 |
|
63 |
This is handy since unset variables evaluate as zero, which is false inside |
64 |
((..)), so a simple flag=1 is all that's needed, ie you don't have to set a |
65 |
default, although ofc it's more robust to do so, especially for functions. |
66 |
declare -i flag=0 # makes a local var which can only have integer values |
67 |
assigned (setting it to string typically evaluates to zero; arithmetic |
68 |
context is used for all assignments, so a string which happens to be a |
69 |
variable with an integer will return that value.) |
70 |
|
71 |
[1] http://wooledge.org/mywiki/ArithmeticExpression |
72 |
|
73 |
|
74 |
-- |
75 |
gentoo-dev@g.o mailing list |