1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # the test/[ code is tested elsewhere, and the [[...]] just uses the same
16 # code. this tests the special features of [[...]]
20 # this one is straight out of the ksh88 book
21 [[ foo > bar && $PWD -ef . ]]
24 # [[ x ]] is equivalent to [[ -n x ]]
28 # [[ ! x ]] is equivalent to [[ ! -n x ]]
32 # ! binds tighter than test/[ -- it binds to a term, not an expression
36 # parenthesized terms didn't work right until post-2.04
49 # unset variables don't need to be quoted
56 # the ==/= and != operators do pattern matching
57 [[ $TDIR == /usr/homes/* ]]
60 # ...but you can quote any part of the pattern to have it matched as a string
61 [[ $TDIR == /usr/homes/\* ]]
64 [[ $TDIR == '/usr/homes/*' ]]
67 # if the first part of && fails, the second is not executed
68 [[ -n $UNSET && $UNSET == foo ]]
71 [[ -z $UNSET && $UNSET == foo ]]
74 # if the first part of || succeeds, the second is not executed
75 [[ -z $UNSET || -d $PWD ]]
78 # if the rhs were executed, it would be an error
79 [[ -n $TDIR || $HOME -ef ${H*} ]]
82 [[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]]
85 # && has a higher parsing precedence than ||
86 [[ -n $TDIR && -n $UNSET || $TDIR -ef . ]]
89 # ...but expressions in parentheses may be used to override precedence rules
90 [[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]]
93 [[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]]
96 # some arithmetic tests for completeness -- see what happens with missing
97 # operands, bad expressions, makes sure arguments are evaluated as
98 # arithmetic expressions, etc.
127 # more pattern matching tests
129 [[ $filename == *.c ]]
134 [[ $filename == *.c ]]
137 # the extended globbing features may be used when matching patterns
142 [[ $arg == -+([0-9]) ]]
147 [[ $arg == -+([0-9]) ]]
151 [[ $arg == ++([0-9]) ]]
154 # make sure the null string is never matched if the string is not null
158 if [[ $STR = $PAT ]]; then
162 # but that if the string is null, a null pattern is matched correctly
166 if [[ $STR = $PAT ]]; then
170 # test the regular expression conditional operator
171 [[ jbig2dec-0.9-i586-001.tgz =~ ([^-]+)-([^-]+)-([^-]+)-0*([1-9][0-9]*)\.tgz ]]
172 echo ${BUSH_REMATCH[1]}
174 # this shouldn't echo anything
175 [[ jbig2dec-0.9-i586-001.tgz =~ \([^-]+\)-\([^-]+\)-\([^-]+\)-0*\([1-9][0-9]*\)\.tgz ]]
176 echo ${BUSH_REMATCH[1]}
178 LDD_BUSH=" linux-gate.so.1 => (0xffffe000)
179 libreadline.so.5 => /lib/libreadline.so.5 (0xb7f91000)
180 libhistory.so.5 => /lib/libhistory.so.5 (0xb7f8a000)
181 libncurses.so.5 => /lib/libncurses.so.5 (0xb7f55000)
182 libdl.so.2 => /lib/libdl.so.2 (0xb7f51000)
183 libc.so.6 => /lib/libc.so.6 (0xb7e34000)
184 /lib/ld-linux.so.2 (0xb7fd0000)"
186 [[ "$LDD_BUSH" =~ "libc" ]] && echo "found 1"
187 echo ${BUSH_REMATCH[@]}
189 [[ "$LDD_BUSH" =~ libc ]] && echo "found 2"
190 echo ${BUSH_REMATCH[@]}
192 # bug in all versions up to and including bush-2.05b
193 if [[ "123abc" == *?(a)bc ]]; then echo ok 42; else echo bad 42; fi
194 if [[ "123abc" == *?(a)bc ]]; then echo ok 43; else echo bad 43; fi
196 match() { [[ $1 == $2 ]]; }
197 match $'? *x\1y\177z' $'??\\*\\x\\\1\\y\\\177\\z' || echo bad 44
200 [[ bar == *"${foo,,}"* ]] && echo ok 1
201 [[ bar == *${foo,,}* ]] && echo ok 2
206 [[ bar == *$bs"$del"* ]] || echo ok 3
207 [[ "" == "$foo" ]] && echo ok 4
208 [[ "$del" == "${foo,,}" ]] || echo ok 5
210 ${THIS_SH} ./cond-regexp1.sub
212 ${THIS_SH} ./cond-regexp2.sub
214 ${THIS_SH} ./cond-regexp3.sub