summaryrefslogtreecommitdiffstats
path: root/tests/cond.tests
blob: aa6a81047eb7c63e861e634679b20c55dcba1778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# the test/[ code is tested elsewhere, and the [[...]] just uses the same
# code.  this tests the special features of [[...]]
#
TDIR=/usr/homes/chet

# this one is straight out of the ksh88 book
[[ foo > bar && $PWD -ef . ]]
echo returns: $?

# [[ x ]] is equivalent to [[ -n x ]]
[[ x ]]
echo returns: $?

# [[ ! x ]] is equivalent to [[ ! -n x ]]
[[ ! x ]]
echo returns: $?

# ! binds tighter than test/[ -- it binds to a term, not an expression
[[ ! x || x ]]
echo returns: $?

# parenthesized terms didn't work right until post-2.04
[[ a ]]
echo returns: $?

[[ (a) ]]
echo returns: $?

[[ -n a ]]
echo returns: $?

[[ (-n a) ]]
echo returns: $?

# unset variables don't need to be quoted
[[ -n $UNSET ]]
echo returns: $?

[[ -z $UNSET ]]
echo returns: $?

# the ==/= and != operators do pattern matching
[[ $TDIR == /usr/homes/* ]]
echo returns: $?

# ...but you can quote any part of the pattern to have it matched as a string
[[ $TDIR == /usr/homes/\* ]]
echo returns: $?

[[ $TDIR == '/usr/homes/*' ]]
echo returns: $?

# if the first part of && fails, the second is not executed
[[ -n $UNSET && $UNSET == foo ]]
echo returns: $?

[[ -z $UNSET && $UNSET == foo ]]
echo returns: $?

# if the first part of || succeeds, the second is not executed
[[ -z $UNSET || -d $PWD ]]
echo returns: $?

# if the rhs were executed, it would be an error
[[ -n $TDIR || $HOME -ef ${H*} ]]
echo returns: $?

[[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]]
echo returns: $?

# && has a higher parsing precedence than ||
[[ -n $TDIR && -n $UNSET || $TDIR -ef . ]]
echo returns: $?

# ...but expressions in parentheses may be used to override precedence rules
[[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]]
echo returns: $?

[[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]]
echo returns: $?

# some arithmetic tests for completeness -- see what happens with missing
# operands, bad expressions, makes sure arguments are evaluated as
# arithmetic expressions, etc.

unset IVAR A
[[ 7 -gt $IVAR ]]
echo returns: $?

[[ $IVAR -gt 7 ]]
echo returns: $?

IVAR=4
[[ $IVAR -gt 7 ]]
echo returns: $?

[[ 7 -eq 4+3 ]]
echo returns: $?

[[ 7 -eq 4+ ]] 
echo returns: $? 

IVAR=4+3
[[ $IVAR -eq 7 ]]
echo returns: $?

A=7
[[ $IVAR -eq A ]]
echo returns: $?

unset IVAR A

# more pattern matching tests

[[ $filename == *.c ]]
echo returns: $?

filename=patmatch.c

[[ $filename == *.c ]]
echo returns: $?

# the extended globbing features may be used when matching patterns
shopt -s extglob

arg=-7

[[ $arg == -+([0-9]) ]]
echo returns: $?

arg=-H

[[ $arg == -+([0-9]) ]]
echo returns: $?

arg=+4
[[ $arg == ++([0-9]) ]]
echo returns: $?

# make sure the null string is never matched if the string is not null
STR=file.c
PAT=

if [[ $STR = $PAT ]]; then
        echo oops
fi

# but that if the string is null, a null pattern is matched correctly
STR=
PAT=

if [[ $STR = $PAT ]]; then
        echo ok
fi

# test the regular expression conditional operator
[[ jbig2dec-0.9-i586-001.tgz =~ ([^-]+)-([^-]+)-([^-]+)-0*([1-9][0-9]*)\.tgz ]]
echo ${BASH_REMATCH[1]}

# this shouldn't echo anything
[[ jbig2dec-0.9-i586-001.tgz =~ \([^-]+\)-\([^-]+\)-\([^-]+\)-0*\([1-9][0-9]*\)\.tgz ]]
echo ${BASH_REMATCH[1]}

LDD_BASH="       linux-gate.so.1 =>  (0xffffe000)
       libreadline.so.5 => /lib/libreadline.so.5 (0xb7f91000)
       libhistory.so.5 => /lib/libhistory.so.5 (0xb7f8a000)
       libncurses.so.5 => /lib/libncurses.so.5 (0xb7f55000)
       libdl.so.2 => /lib/libdl.so.2 (0xb7f51000)
       libc.so.6 => /lib/libc.so.6 (0xb7e34000)
       /lib/ld-linux.so.2 (0xb7fd0000)"

[[ "$LDD_BASH" =~ "libc" ]] && echo "found 1" 
echo ${BASH_REMATCH[@]}

[[ "$LDD_BASH" =~ libc ]] && echo "found 2" 
echo ${BASH_REMATCH[@]}

# bug in all versions up to and including bash-2.05b
if [[ "123abc" == *?(a)bc ]]; then echo ok 42; else echo bad 42; fi
if [[ "123abc" == *?(a)bc ]]; then echo ok 43; else echo bad 43; fi

match() { [[ $1 == $2 ]]; }
match $'? *x\1y\177z' $'??\\*\\x\\\1\\y\\\177\\z' || echo bad 44

foo=""
[[ bar == *"${foo,,}"* ]] && echo ok 1
[[ bar == *${foo,,}* ]] && echo ok 2

shopt -s extquote
bs='\'
del=$'\177'
[[ bar == *$bs"$del"* ]] || echo ok 3
[[ "" == "$foo" ]] && echo ok 4
[[ "$del" == "${foo,,}" ]] || echo ok 5

${THIS_SH} ./cond-regexp1.sub

${THIS_SH} ./cond-regexp2.sub

${THIS_SH} ./cond-regexp3.sub