2 # the test/[ code is tested elsewhere, and the [[...]] just uses the same
3 # code. this tests the special features of [[...]]
7 # this one is straight out of the ksh88 book
8 [[ foo > bar && $PWD -ef . ]]
11 # [[ x ]] is equivalent to [[ -n x ]]
15 # [[ ! x ]] is equivalent to [[ ! -n x ]]
19 # ! binds tighter than test/[ -- it binds to a term, not an expression
23 # parenthesized terms didn't work right until post-2.04
36 # unset variables don't need to be quoted
43 # the ==/= and != operators do pattern matching
44 [[ $TDIR == /usr/homes/* ]]
47 # ...but you can quote any part of the pattern to have it matched as a string
48 [[ $TDIR == /usr/homes/\* ]]
51 [[ $TDIR == '/usr/homes/*' ]]
54 # if the first part of && fails, the second is not executed
55 [[ -n $UNSET && $UNSET == foo ]]
58 [[ -z $UNSET && $UNSET == foo ]]
61 # if the first part of || succeeds, the second is not executed
62 [[ -z $UNSET || -d $PWD ]]
65 # if the rhs were executed, it would be an error
66 [[ -n $TDIR || $HOME -ef ${H*} ]]
69 [[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]]
72 # && has a higher parsing precedence than ||
73 [[ -n $TDIR && -n $UNSET || $TDIR -ef . ]]
76 # ...but expressions in parentheses may be used to override precedence rules
77 [[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]]
80 [[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]]
83 # some arithmetic tests for completeness -- see what happens with missing
84 # operands, bad expressions, makes sure arguments are evaluated as
85 # arithmetic expressions, etc.
114 # more pattern matching tests
116 [[ $filename == *.c ]]
121 [[ $filename == *.c ]]
124 # the extended globbing features may be used when matching patterns
129 [[ $arg == -+([0-9]) ]]
134 [[ $arg == -+([0-9]) ]]
138 [[ $arg == ++([0-9]) ]]
141 # make sure the null string is never matched if the string is not null
145 if [[ $STR = $PAT ]]; then
149 # but that if the string is null, a null pattern is matched correctly
153 if [[ $STR = $PAT ]]; then
157 # test the regular expression conditional operator
158 [[ jbig2dec-0.9-i586-001.tgz =~ ([^-]+)-([^-]+)-([^-]+)-0*([1-9][0-9]*)\.tgz ]]
159 echo ${BASH_REMATCH[1]}
161 # this shouldn't echo anything
162 [[ jbig2dec-0.9-i586-001.tgz =~ \([^-]+\)-\([^-]+\)-\([^-]+\)-0*\([1-9][0-9]*\)\.tgz ]]
163 echo ${BASH_REMATCH[1]}
165 LDD_BASH=" linux-gate.so.1 => (0xffffe000)
166 libreadline.so.5 => /lib/libreadline.so.5 (0xb7f91000)
167 libhistory.so.5 => /lib/libhistory.so.5 (0xb7f8a000)
168 libncurses.so.5 => /lib/libncurses.so.5 (0xb7f55000)
169 libdl.so.2 => /lib/libdl.so.2 (0xb7f51000)
170 libc.so.6 => /lib/libc.so.6 (0xb7e34000)
171 /lib/ld-linux.so.2 (0xb7fd0000)"
173 [[ "$LDD_BASH" =~ "libc" ]] && echo "found 1"
174 echo ${BASH_REMATCH[@]}
176 [[ "$LDD_BASH" =~ libc ]] && echo "found 2"
177 echo ${BASH_REMATCH[@]}
179 # bug in all versions up to and including bash-2.05b
180 if [[ "123abc" == *?(a)bc ]]; then echo ok 42; else echo bad 42; fi
181 if [[ "123abc" == *?(a)bc ]]; then echo ok 43; else echo bad 43; fi
183 ${THIS_SH} ./cond-regexp1.sub
185 ${THIS_SH} ./cond-regexp2.sub
187 ${THIS_SH} ./cond-regexp3.sub