]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/cond.tests
bash-4.3-rc2 overlay
[thirdparty/bash.git] / tests / cond.tests
CommitLineData
cce855bc
JA
1#
2# the test/[ code is tested elsewhere, and the [[...]] just uses the same
3# code. this tests the special features of [[...]]
4#
5TDIR=/usr/homes/chet
6
7# this one is straight out of the ksh88 book
8[[ foo > bar && $PWD -ef . ]]
9echo returns: $?
10
11# [[ x ]] is equivalent to [[ -n x ]]
12[[ x ]]
13echo returns: $?
14
15# [[ ! x ]] is equivalent to [[ ! -n x ]]
16[[ ! x ]]
17echo returns: $?
18
19# ! binds tighter than test/[ -- it binds to a term, not an expression
20[[ ! x || x ]]
21echo returns: $?
22
28ef6c31
JA
23# parenthesized terms didn't work right until post-2.04
24[[ a ]]
25echo returns: $?
26
27[[ (a) ]]
28echo returns: $?
29
30[[ -n a ]]
31echo returns: $?
32
33[[ (-n a) ]]
34echo returns: $?
35
cce855bc
JA
36# unset variables don't need to be quoted
37[[ -n $UNSET ]]
38echo returns: $?
39
40[[ -z $UNSET ]]
41echo returns: $?
42
43# the ==/= and != operators do pattern matching
44[[ $TDIR == /usr/homes/* ]]
45echo returns: $?
46
47# ...but you can quote any part of the pattern to have it matched as a string
48[[ $TDIR == /usr/homes/\* ]]
49echo returns: $?
50
51[[ $TDIR == '/usr/homes/*' ]]
52echo returns: $?
53
54# if the first part of && fails, the second is not executed
55[[ -n $UNSET && $UNSET == foo ]]
56echo returns: $?
57
58[[ -z $UNSET && $UNSET == foo ]]
59echo returns: $?
60
61# if the first part of || succeeds, the second is not executed
62[[ -z $UNSET || -d $PWD ]]
63echo returns: $?
64
65# if the rhs were executed, it would be an error
66[[ -n $TDIR || $HOME -ef ${H*} ]]
67echo returns: $?
68
69[[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]]
70echo returns: $?
71
72# && has a higher parsing precedence than ||
73[[ -n $TDIR && -n $UNSET || $TDIR -ef . ]]
74echo returns: $?
75
76# ...but expressions in parentheses may be used to override precedence rules
77[[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]]
78echo returns: $?
79
80[[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]]
81echo returns: $?
82
83# some arithmetic tests for completeness -- see what happens with missing
84# operands, bad expressions, makes sure arguments are evaluated as
85# arithmetic expressions, etc.
86
87unset IVAR A
88[[ 7 -gt $IVAR ]]
89echo returns: $?
90
91[[ $IVAR -gt 7 ]]
92echo returns: $?
93
94IVAR=4
95[[ $IVAR -gt 7 ]]
96echo returns: $?
97
98[[ 7 -eq 4+3 ]]
99echo returns: $?
100
101[[ 7 -eq 4+ ]]
102echo returns: $?
103
104IVAR=4+3
105[[ $IVAR -eq 7 ]]
106echo returns: $?
107
108A=7
109[[ $IVAR -eq A ]]
110echo returns: $?
111
112unset IVAR A
113
114# more pattern matching tests
115
116[[ $filename == *.c ]]
117echo returns: $?
118
119filename=patmatch.c
120
121[[ $filename == *.c ]]
122echo returns: $?
123
124# the extended globbing features may be used when matching patterns
125shopt -s extglob
126
127arg=-7
128
129[[ $arg == -+([0-9]) ]]
130echo returns: $?
131
132arg=-H
133
134[[ $arg == -+([0-9]) ]]
135echo returns: $?
136
137arg=+4
138[[ $arg == ++([0-9]) ]]
139echo returns: $?
140
141# make sure the null string is never matched if the string is not null
142STR=file.c
143PAT=
144
145if [[ $STR = $PAT ]]; then
146 echo oops
147fi
148
149# but that if the string is null, a null pattern is matched correctly
150STR=
151PAT=
152
153if [[ $STR = $PAT ]]; then
154 echo ok
155fi
d3a24ed2 156
d3ad40de
CR
157# test the regular expression conditional operator
158[[ jbig2dec-0.9-i586-001.tgz =~ ([^-]+)-([^-]+)-([^-]+)-0*([1-9][0-9]*)\.tgz ]]
159echo ${BASH_REMATCH[1]}
160
8943768b
CR
161# this shouldn't echo anything
162[[ jbig2dec-0.9-i586-001.tgz =~ \([^-]+\)-\([^-]+\)-\([^-]+\)-0*\([1-9][0-9]*\)\.tgz ]]
163echo ${BASH_REMATCH[1]}
164
d3ad40de
CR
165LDD_BASH=" linux-gate.so.1 => (0xffffe000)
166 libreadline.so.5 => /lib/libreadline.so.5 (0xb7f91000)
167 libhistory.so.5 => /lib/libhistory.so.5 (0xb7f8a000)
168 libncurses.so.5 => /lib/libncurses.so.5 (0xb7f55000)
169 libdl.so.2 => /lib/libdl.so.2 (0xb7f51000)
170 libc.so.6 => /lib/libc.so.6 (0xb7e34000)
171 /lib/ld-linux.so.2 (0xb7fd0000)"
172
173[[ "$LDD_BASH" =~ "libc" ]] && echo "found 1"
174echo ${BASH_REMATCH[@]}
175
176[[ "$LDD_BASH" =~ libc ]] && echo "found 2"
177echo ${BASH_REMATCH[@]}
178
d3a24ed2
CR
179# bug in all versions up to and including bash-2.05b
180if [[ "123abc" == *?(a)bc ]]; then echo ok 42; else echo bad 42; fi
181if [[ "123abc" == *?(a)bc ]]; then echo ok 43; else echo bad 43; fi
d3ad40de 182
4b82d1cd
CR
183${THIS_SH} ./cond-regexp1.sub
184
185${THIS_SH} ./cond-regexp2.sub
b6e23235
CR
186
187${THIS_SH} ./cond-regexp3.sub