]>
Commit | Line | Data |
---|---|---|
8868edaf CR |
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. | |
5 | # | |
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. | |
10 | # | |
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/>. | |
13 | # | |
cce855bc JA |
14 | # |
15 | # the test/[ code is tested elsewhere, and the [[...]] just uses the same | |
16 | # code. this tests the special features of [[...]] | |
17 | # | |
18 | TDIR=/usr/homes/chet | |
19 | ||
20 | # this one is straight out of the ksh88 book | |
21 | [[ foo > bar && $PWD -ef . ]] | |
22 | echo returns: $? | |
23 | ||
24 | # [[ x ]] is equivalent to [[ -n x ]] | |
25 | [[ x ]] | |
26 | echo returns: $? | |
27 | ||
28 | # [[ ! x ]] is equivalent to [[ ! -n x ]] | |
29 | [[ ! x ]] | |
30 | echo returns: $? | |
31 | ||
32 | # ! binds tighter than test/[ -- it binds to a term, not an expression | |
33 | [[ ! x || x ]] | |
34 | echo returns: $? | |
35 | ||
28ef6c31 JA |
36 | # parenthesized terms didn't work right until post-2.04 |
37 | [[ a ]] | |
38 | echo returns: $? | |
39 | ||
40 | [[ (a) ]] | |
41 | echo returns: $? | |
42 | ||
43 | [[ -n a ]] | |
44 | echo returns: $? | |
45 | ||
46 | [[ (-n a) ]] | |
47 | echo returns: $? | |
48 | ||
cce855bc JA |
49 | # unset variables don't need to be quoted |
50 | [[ -n $UNSET ]] | |
51 | echo returns: $? | |
52 | ||
53 | [[ -z $UNSET ]] | |
54 | echo returns: $? | |
55 | ||
56 | # the ==/= and != operators do pattern matching | |
57 | [[ $TDIR == /usr/homes/* ]] | |
58 | echo returns: $? | |
59 | ||
60 | # ...but you can quote any part of the pattern to have it matched as a string | |
61 | [[ $TDIR == /usr/homes/\* ]] | |
62 | echo returns: $? | |
63 | ||
64 | [[ $TDIR == '/usr/homes/*' ]] | |
65 | echo returns: $? | |
66 | ||
67 | # if the first part of && fails, the second is not executed | |
68 | [[ -n $UNSET && $UNSET == foo ]] | |
69 | echo returns: $? | |
70 | ||
71 | [[ -z $UNSET && $UNSET == foo ]] | |
72 | echo returns: $? | |
73 | ||
74 | # if the first part of || succeeds, the second is not executed | |
75 | [[ -z $UNSET || -d $PWD ]] | |
76 | echo returns: $? | |
77 | ||
78 | # if the rhs were executed, it would be an error | |
79 | [[ -n $TDIR || $HOME -ef ${H*} ]] | |
80 | echo returns: $? | |
81 | ||
82 | [[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]] | |
83 | echo returns: $? | |
84 | ||
85 | # && has a higher parsing precedence than || | |
86 | [[ -n $TDIR && -n $UNSET || $TDIR -ef . ]] | |
87 | echo returns: $? | |
88 | ||
89 | # ...but expressions in parentheses may be used to override precedence rules | |
90 | [[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]] | |
91 | echo returns: $? | |
92 | ||
93 | [[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]] | |
94 | echo returns: $? | |
95 | ||
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. | |
99 | ||
100 | unset IVAR A | |
101 | [[ 7 -gt $IVAR ]] | |
102 | echo returns: $? | |
103 | ||
104 | [[ $IVAR -gt 7 ]] | |
105 | echo returns: $? | |
106 | ||
107 | IVAR=4 | |
108 | [[ $IVAR -gt 7 ]] | |
109 | echo returns: $? | |
110 | ||
111 | [[ 7 -eq 4+3 ]] | |
112 | echo returns: $? | |
113 | ||
114 | [[ 7 -eq 4+ ]] | |
115 | echo returns: $? | |
116 | ||
117 | IVAR=4+3 | |
118 | [[ $IVAR -eq 7 ]] | |
119 | echo returns: $? | |
120 | ||
121 | A=7 | |
122 | [[ $IVAR -eq A ]] | |
123 | echo returns: $? | |
124 | ||
125 | unset IVAR A | |
126 | ||
127 | # more pattern matching tests | |
128 | ||
129 | [[ $filename == *.c ]] | |
130 | echo returns: $? | |
131 | ||
132 | filename=patmatch.c | |
133 | ||
134 | [[ $filename == *.c ]] | |
135 | echo returns: $? | |
136 | ||
137 | # the extended globbing features may be used when matching patterns | |
138 | shopt -s extglob | |
139 | ||
140 | arg=-7 | |
141 | ||
142 | [[ $arg == -+([0-9]) ]] | |
143 | echo returns: $? | |
144 | ||
145 | arg=-H | |
146 | ||
147 | [[ $arg == -+([0-9]) ]] | |
148 | echo returns: $? | |
149 | ||
150 | arg=+4 | |
151 | [[ $arg == ++([0-9]) ]] | |
152 | echo returns: $? | |
153 | ||
154 | # make sure the null string is never matched if the string is not null | |
155 | STR=file.c | |
156 | PAT= | |
157 | ||
158 | if [[ $STR = $PAT ]]; then | |
159 | echo oops | |
160 | fi | |
161 | ||
162 | # but that if the string is null, a null pattern is matched correctly | |
163 | STR= | |
164 | PAT= | |
165 | ||
166 | if [[ $STR = $PAT ]]; then | |
167 | echo ok | |
168 | fi | |
b80f6443 | 169 | |
3185942a JA |
170 | # test the regular expression conditional operator |
171 | [[ jbig2dec-0.9-i586-001.tgz =~ ([^-]+)-([^-]+)-([^-]+)-0*([1-9][0-9]*)\.tgz ]] | |
172 | echo ${BASH_REMATCH[1]} | |
173 | ||
174 | # this shouldn't echo anything | |
175 | [[ jbig2dec-0.9-i586-001.tgz =~ \([^-]+\)-\([^-]+\)-\([^-]+\)-0*\([1-9][0-9]*\)\.tgz ]] | |
176 | echo ${BASH_REMATCH[1]} | |
177 | ||
178 | LDD_BASH=" 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)" | |
185 | ||
186 | [[ "$LDD_BASH" =~ "libc" ]] && echo "found 1" | |
187 | echo ${BASH_REMATCH[@]} | |
188 | ||
189 | [[ "$LDD_BASH" =~ libc ]] && echo "found 2" | |
190 | echo ${BASH_REMATCH[@]} | |
191 | ||
b80f6443 JA |
192 | # bug in all versions up to and including bash-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 | |
3185942a | 195 | |
d233b485 CR |
196 | match() { [[ $1 == $2 ]]; } |
197 | match $'? *x\1y\177z' $'??\\*\\x\\\1\\y\\\177\\z' || echo bad 44 | |
198 | ||
a0c0a00f CR |
199 | foo="" |
200 | [[ bar == *"${foo,,}"* ]] && echo ok 1 | |
201 | [[ bar == *${foo,,}* ]] && echo ok 2 | |
202 | ||
203 | shopt -s extquote | |
204 | bs='\' | |
205 | del=$'\177' | |
206 | [[ bar == *$bs"$del"* ]] || echo ok 3 | |
207 | [[ "" == "$foo" ]] && echo ok 4 | |
208 | [[ "$del" == "${foo,,}" ]] || echo ok 5 | |
209 | ||
ac50fbac CR |
210 | ${THIS_SH} ./cond-regexp1.sub |
211 | ||
212 | ${THIS_SH} ./cond-regexp2.sub | |
213 | ||
214 | ${THIS_SH} ./cond-regexp3.sub |