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 # ! toggles on and off rather than just setting an `invert result' flag
37 # this differs from ksh93
38 [[ ! 1 -eq 1 ]]; echo $?
39 [[ ! ! 1 -eq 1 ]]; echo $?
41 [[ ! ! ! 1 -eq 1 ]]; echo $?
42 [[ ! ! ! ! 1 -eq 1 ]]; echo $?
44 # parenthesized terms didn't work right until post-2.04
57 # unset variables don't need to be quoted
64 # the ==/= and != operators do pattern matching
65 [[ $TDIR == /usr/homes/* ]]
68 # ...but you can quote any part of the pattern to have it matched as a string
69 [[ $TDIR == /usr/homes/\* ]]
72 [[ $TDIR == '/usr/homes/*' ]]
75 # if the first part of && fails, the second is not executed
76 [[ -n $UNSET && $UNSET == foo ]]
79 [[ -z $UNSET && $UNSET == foo ]]
82 # if the first part of || succeeds, the second is not executed
83 [[ -z $UNSET || -d $PWD ]]
86 # if the rhs were executed, it would be an error
87 [[ -n $TDIR || $HOME -ef ${H*} ]]
90 [[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]]
93 # && has a higher parsing precedence than ||
94 [[ -n $TDIR && -n $UNSET || $TDIR -ef . ]]
97 # ...but expressions in parentheses may be used to override precedence rules
98 [[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]]
101 [[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]]
104 # some arithmetic tests for completeness -- see what happens with missing
105 # operands, bad expressions, makes sure arguments are evaluated as
106 # arithmetic expressions, etc.
133 [[ "$IVAR" -eq "7" ]]
137 [[ "$IVAR" -eq "A" ]]
142 # more pattern matching tests
144 [[ $filename == *.c ]]
149 [[ $filename == *.c ]]
152 # the extended globbing features may be used when matching patterns
157 [[ $arg == -+([0-9]) ]]
162 [[ $arg == -+([0-9]) ]]
166 [[ $arg == ++([0-9]) ]]
169 # make sure the null string is never matched if the string is not null
173 if [[ $STR = $PAT ]]; then
177 # but that if the string is null, a null pattern is matched correctly
181 if [[ $STR = $PAT ]]; then
185 # test the regular expression conditional operator
186 [[ jbig2dec-0.9-i586-001.tgz =~ ([^-]+)-([^-]+)-([^-]+)-0*([1-9][0-9]*)\.tgz ]]
187 echo ${BASH_REMATCH[1]}
189 # this shouldn't echo anything
190 [[ jbig2dec-0.9-i586-001.tgz =~ \([^-]+\)-\([^-]+\)-\([^-]+\)-0*\([1-9][0-9]*\)\.tgz ]]
191 echo ${BASH_REMATCH[1]}
193 LDD_BASH=" linux-gate.so.1 => (0xffffe000)
194 libreadline.so.5 => /lib/libreadline.so.5 (0xb7f91000)
195 libhistory.so.5 => /lib/libhistory.so.5 (0xb7f8a000)
196 libncurses.so.5 => /lib/libncurses.so.5 (0xb7f55000)
197 libdl.so.2 => /lib/libdl.so.2 (0xb7f51000)
198 libc.so.6 => /lib/libc.so.6 (0xb7e34000)
199 /lib/ld-linux.so.2 (0xb7fd0000)"
201 [[ "$LDD_BASH" =~ "libc" ]] && echo "found 1"
202 echo ${BASH_REMATCH[@]}
204 [[ "$LDD_BASH" =~ libc ]] && echo "found 2"
205 echo ${BASH_REMATCH[@]}
207 # bug in all versions up to and including bash-2.05b
208 if [[ "123abc" == *?(a)bc ]]; then echo ok 42; else echo bad 42; fi
209 if [[ "123abc" == *?(a)bc ]]; then echo ok 43; else echo bad 43; fi
211 match() { [[ $1 == $2 ]]; }
212 match $'? *x\1y\177z' $'??\\*\\x\\\1\\y\\\177\\z' || echo bad 44
215 [[ bar == *"${foo,,}"* ]] && echo ok 1
216 [[ bar == *${foo,,}* ]] && echo ok 2
221 [[ bar == *$bs"$del"* ]] || echo ok 3
222 [[ "" == "$foo" ]] && echo ok 4
223 [[ "$del" == "${foo,,}" ]] || echo ok 5
225 # allow reserved words after a conditional command just because
226 if [[ str ]] then [[ str ]] fi
228 ${THIS_SH} ./cond-regexp1.sub
230 ${THIS_SH} ./cond-regexp2.sub
232 ${THIS_SH} ./cond-regexp3.sub