]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/cond.tests
Bash-5.2-rc4 release
[thirdparty/bash.git] / tests / cond.tests
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 #
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
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 $?
40
41 [[ ! ! ! 1 -eq 1 ]]; echo $?
42 [[ ! ! ! ! 1 -eq 1 ]]; echo $?
43
44 # parenthesized terms didn't work right until post-2.04
45 [[ a ]]
46 echo returns: $?
47
48 [[ (a) ]]
49 echo returns: $?
50
51 [[ -n a ]]
52 echo returns: $?
53
54 [[ (-n a) ]]
55 echo returns: $?
56
57 # unset variables don't need to be quoted
58 [[ -n $UNSET ]]
59 echo returns: $?
60
61 [[ -z $UNSET ]]
62 echo returns: $?
63
64 # the ==/= and != operators do pattern matching
65 [[ $TDIR == /usr/homes/* ]]
66 echo returns: $?
67
68 # ...but you can quote any part of the pattern to have it matched as a string
69 [[ $TDIR == /usr/homes/\* ]]
70 echo returns: $?
71
72 [[ $TDIR == '/usr/homes/*' ]]
73 echo returns: $?
74
75 # if the first part of && fails, the second is not executed
76 [[ -n $UNSET && $UNSET == foo ]]
77 echo returns: $?
78
79 [[ -z $UNSET && $UNSET == foo ]]
80 echo returns: $?
81
82 # if the first part of || succeeds, the second is not executed
83 [[ -z $UNSET || -d $PWD ]]
84 echo returns: $?
85
86 # if the rhs were executed, it would be an error
87 [[ -n $TDIR || $HOME -ef ${H*} ]]
88 echo returns: $?
89
90 [[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]]
91 echo returns: $?
92
93 # && has a higher parsing precedence than ||
94 [[ -n $TDIR && -n $UNSET || $TDIR -ef . ]]
95 echo returns: $?
96
97 # ...but expressions in parentheses may be used to override precedence rules
98 [[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]]
99 echo returns: $?
100
101 [[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]]
102 echo returns: $?
103
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.
107
108 unset IVAR A
109 [[ 7 -gt $IVAR ]]
110 echo returns: $?
111
112 [[ $IVAR -gt 7 ]]
113 echo returns: $?
114
115 IVAR=4
116 [[ $IVAR -gt 7 ]]
117 echo returns: $?
118
119 [[ 7 -eq 4+3 ]]
120 echo returns: $?
121
122 [[ 7 -eq 4+ ]]
123 echo returns: $?
124
125 IVAR=4+3
126 [[ $IVAR -eq 7 ]]
127 echo returns: $?
128
129 A=7
130 [[ $IVAR -eq A ]]
131 echo returns: $?
132
133 [[ "$IVAR" -eq "7" ]]
134 echo returns: $?
135
136 A=7
137 [[ "$IVAR" -eq "A" ]]
138 echo returns: $?
139
140 unset IVAR A
141
142 # more pattern matching tests
143
144 [[ $filename == *.c ]]
145 echo returns: $?
146
147 filename=patmatch.c
148
149 [[ $filename == *.c ]]
150 echo returns: $?
151
152 # the extended globbing features may be used when matching patterns
153 shopt -s extglob
154
155 arg=-7
156
157 [[ $arg == -+([0-9]) ]]
158 echo returns: $?
159
160 arg=-H
161
162 [[ $arg == -+([0-9]) ]]
163 echo returns: $?
164
165 arg=+4
166 [[ $arg == ++([0-9]) ]]
167 echo returns: $?
168
169 # make sure the null string is never matched if the string is not null
170 STR=file.c
171 PAT=
172
173 if [[ $STR = $PAT ]]; then
174 echo oops
175 fi
176
177 # but that if the string is null, a null pattern is matched correctly
178 STR=
179 PAT=
180
181 if [[ $STR = $PAT ]]; then
182 echo ok
183 fi
184
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]}
188
189 # this shouldn't echo anything
190 [[ jbig2dec-0.9-i586-001.tgz =~ \([^-]+\)-\([^-]+\)-\([^-]+\)-0*\([1-9][0-9]*\)\.tgz ]]
191 echo ${BASH_REMATCH[1]}
192
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)"
200
201 [[ "$LDD_BASH" =~ "libc" ]] && echo "found 1"
202 echo ${BASH_REMATCH[@]}
203
204 [[ "$LDD_BASH" =~ libc ]] && echo "found 2"
205 echo ${BASH_REMATCH[@]}
206
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
210
211 match() { [[ $1 == $2 ]]; }
212 match $'? *x\1y\177z' $'??\\*\\x\\\1\\y\\\177\\z' || echo bad 44
213
214 foo=""
215 [[ bar == *"${foo,,}"* ]] && echo ok 1
216 [[ bar == *${foo,,}* ]] && echo ok 2
217
218 shopt -s extquote
219 bs='\'
220 del=$'\177'
221 [[ bar == *$bs"$del"* ]] || echo ok 3
222 [[ "" == "$foo" ]] && echo ok 4
223 [[ "$del" == "${foo,,}" ]] || echo ok 5
224
225 # allow reserved words after a conditional command just because
226 if [[ str ]] then [[ str ]] fi
227
228 ${THIS_SH} ./cond-regexp1.sub
229
230 ${THIS_SH} ./cond-regexp2.sub
231
232 ${THIS_SH} ./cond-regexp3.sub