Imported from ../bash-2.02.tar.gz.
[platform/upstream/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 # unset variables don't need to be quoted
24 [[ -n $UNSET ]]
25 echo returns: $?
26
27 [[ -z $UNSET ]]
28 echo returns: $?
29
30 # the ==/= and != operators do pattern matching
31 [[ $TDIR == /usr/homes/* ]]
32 echo returns: $?
33
34 # ...but you can quote any part of the pattern to have it matched as a string
35 [[ $TDIR == /usr/homes/\* ]]
36 echo returns: $?
37
38 [[ $TDIR == '/usr/homes/*' ]]
39 echo returns: $?
40
41 # if the first part of && fails, the second is not executed
42 [[ -n $UNSET && $UNSET == foo ]]
43 echo returns: $?
44
45 [[ -z $UNSET && $UNSET == foo ]]
46 echo returns: $?
47
48 # if the first part of || succeeds, the second is not executed
49 [[ -z $UNSET || -d $PWD ]]
50 echo returns: $?
51
52 # if the rhs were executed, it would be an error
53 [[ -n $TDIR || $HOME -ef ${H*} ]]
54 echo returns: $?
55
56 [[ -n $TDIR && -z $UNSET || $HOME -ef ${H*} ]]
57 echo returns: $?
58
59 # && has a higher parsing precedence than ||
60 [[ -n $TDIR && -n $UNSET || $TDIR -ef . ]]
61 echo returns: $?
62
63 # ...but expressions in parentheses may be used to override precedence rules
64 [[ -n $TDIR || -n $UNSET && $PWD -ef xyz ]]
65 echo returns: $?
66
67 [[ ( -n $TDIR || -n $UNSET ) && $PWD -ef xyz ]]
68 echo returns: $?
69
70 # some arithmetic tests for completeness -- see what happens with missing
71 # operands, bad expressions, makes sure arguments are evaluated as
72 # arithmetic expressions, etc.
73
74 unset IVAR A
75 [[ 7 -gt $IVAR ]]
76 echo returns: $?
77
78 [[ $IVAR -gt 7 ]]
79 echo returns: $?
80
81 IVAR=4
82 [[ $IVAR -gt 7 ]]
83 echo returns: $?
84
85 [[ 7 -eq 4+3 ]]
86 echo returns: $?
87
88 [[ 7 -eq 4+ ]] 
89 echo returns: $? 
90
91 IVAR=4+3
92 [[ $IVAR -eq 7 ]]
93 echo returns: $?
94
95 A=7
96 [[ $IVAR -eq A ]]
97 echo returns: $?
98
99 unset IVAR A
100
101 # more pattern matching tests
102
103 [[ $filename == *.c ]]
104 echo returns: $?
105
106 filename=patmatch.c
107
108 [[ $filename == *.c ]]
109 echo returns: $?
110
111 # the extended globbing features may be used when matching patterns
112 shopt -s extglob
113
114 arg=-7
115
116 [[ $arg == -+([0-9]) ]]
117 echo returns: $?
118
119 arg=-H
120
121 [[ $arg == -+([0-9]) ]]
122 echo returns: $?
123
124 arg=+4
125 [[ $arg == ++([0-9]) ]]
126 echo returns: $?
127
128 # make sure the null string is never matched if the string is not null
129 STR=file.c
130 PAT=
131
132 if [[ $STR = $PAT ]]; then
133         echo oops
134 fi
135
136 # but that if the string is null, a null pattern is matched correctly
137 STR=
138 PAT=
139
140 if [[ $STR = $PAT ]]; then
141         echo ok
142 fi