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