1 |
On Tuesday, January 15, 2013 03:20:20 AM Duncan wrote: |
2 |
> Mike Frysinger posted on Mon, 14 Jan 2013 17:09:51 -0500 as excerpted: |
3 |
> |
4 |
> >>> + [[ ${BUILD_DIR} ]] || die "${FUNCNAME}: BUILD_DIR not set." |
5 |
> >> |
6 |
> >> really should use -n there |
7 |
> > |
8 |
> >> Doesn't matter. |
9 |
> > |
10 |
> > the point wasn't "will it work". it's more "how easy is it to glance at |
11 |
> > code and know what it is doing". |
12 |
> |
13 |
> Indeed. But arguably standalone [[ ${var} ]] tests ARE easier to "know |
14 |
> what it doing." |
15 |
|
16 |
While I agree, I wouldn't lose sleep over it. Both are perfectly acceptable |
17 |
and nearly equal in clarity. [[ $x ]] is probably most common. |
18 |
|
19 |
> Consider: |
20 |
> ... |
21 |
> 4) You are arguing the "at a glance" position, but didn't even MENTION |
22 |
> the needlessly negated logic of [[ -n $string ]] || ... , which could be |
23 |
> rewritten to avoid the || negation as [[ -z $string ]] && ... |
24 |
|
25 |
-z is the negation. -n is the default. [[ $x ]] is rewritten internally as [[ |
26 |
-n $x ]]. [[ ! $x ]] == [[ ! -n $x ]] == [[ -z $x ]]. It's perfectly logical |
27 |
that this expression evaluates true when given a non-empty value. As ''expr || |
28 |
die'' is the most expected form for error-checking, negating this with ''-z'' |
29 |
or otherwise makes little sense. |
30 |
|
31 |
"Empty == False" is not an uncommon concept. e.g. python -c 'print(bool(""))' |
32 |
|
33 |
> 5) [[ ]] is already a bashism while the standalone string test is common |
34 |
> shell. Surely you're not arguing that people familiar enough with the |
35 |
> [[ ]] || construct to parse it at a glance can't equally capably parse |
36 |
> the a standalone string test, given its use in non-bash shell context as |
37 |
> well. |
38 |
|
39 |
I wish people would stop referring to features that predate both Bash and |
40 |
POSIX as "Bashisms". ''[['' is superior to and easier to understand than |
41 |
''[''. Everyone should be familiar with both. As only Bash is relevant here, |
42 |
''test'' and ''['' are mostly irrelevant. With few exceptions, ''[['' should |
43 |
be preferred when available (and in this case, guaranteed available). |
44 |
|
45 |
> Obviously the example in #6 is taking it a bit far, but my point is, an |
46 |
> explicit (if one-of-many) -z/-n test vs. the standalone (single-case) |
47 |
> $string test really is personal preference. |
48 |
|
49 |
The performance difference is considerable. |
50 |
|
51 |
-- |
52 |
Dan Douglas |