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