]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/builtins.tests
00ebc0fddd40b6152aceac42b01a8ea4f3455a99
[thirdparty/bash.git] / tests / builtins.tests
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 #
14 # tests for miscellaneous builtins not tested elsewhere
15 set +p
16 set +o posix
17
18 ulimit -c 0 2>/dev/null
19
20 # check that break breaks loops
21 for i in a b c; do echo $i; break; echo bad-$i; done
22 echo end-1
23 for i in a b c; do echo $i; break 1; echo bad-$i; done
24 echo end-2
25 for i in a b c; do
26 for j in x y z; do
27 echo $i:$j
28 break
29 echo bad-$i
30 done
31 echo end-$i
32 done
33 echo end-3
34
35 # check that break breaks nested loops
36 for i in a b c; do
37 for j in x y z; do
38 echo $i:$j
39 break 2
40 echo bad-$i
41 done
42 echo end-$i
43 done
44 echo end
45
46 # check that continue continues loops
47 for i in a b c; do echo $i; continue; echo bad-$i ; done
48 echo end-1
49 for i in a b c; do echo $i; continue 1; echo bad-$i; done
50 echo end-2
51 for i in a b c; do
52 for j in x y z; do
53 echo $i:$j
54 continue
55 echo bad-$i-$j
56 done
57 echo end-$i
58 done
59 echo end-3
60
61 # check that continue breaks out of nested loops
62 for i in a b c; do
63 for j in x y z; do
64 echo $i:$j
65 continue 2
66 echo bad-$i-$j
67 done
68 echo end-$i
69 done
70 echo end
71
72 # check that `eval' re-evaluates arguments, but `builtin' and `command' do not
73 AVAR='$BVAR'
74 BVAR=foo
75
76 echo $AVAR
77 builtin echo $AVAR
78 command echo $AVAR
79 eval echo \$AVAR
80 eval echo $AVAR
81
82 # test out eval with a temp environment
83 AVAR=bar eval echo \$AVAR
84 BVAR=xxx eval echo $AVAR
85
86 unset -v AVAR BVAR
87
88 # test umask
89 mask=$(umask)
90 umask 022
91 umask
92 umask -S
93 umask -S u=rwx,g=rwx,o=rx >/dev/null # 002
94 umask
95 umask -S
96 umask -p
97 umask -p -S
98 umask 0
99 umask -S
100 umask ${mask} # restore original mask
101
102 # builtin/command without arguments should do nothing. maybe someday they will
103 builtin
104 command
105
106 # test enable
107 enable -ps
108
109 enable -aps ; enable -nps
110
111 enable -n test
112 case "$(type -t test)" in
113 builtin) echo oops -- enable -n test failed ;;
114 *) echo enable -n test worked ;;
115 esac
116
117 enable test
118 case "$(type -t test)" in
119 builtin) echo enable test worked ;;
120 *) echo oops -- enable test failed ;;
121 esac
122
123 # test options to exec
124 (exec -a specialname ${THIS_SH} -c 'echo $0' )
125 (exec -l -a specialname ${THIS_SH} -c 'echo $0' )
126 # test `clean' environment. if /bin/sh is bash, and the script version of
127 # printenv is run, there will be variables in the environment that bash
128 # sets on startup. Also test code that prefixes argv[0] with a dash.
129 (export FOO=BAR ; exec -c -l printenv ) | grep FOO
130 (FOO=BAR exec -c printenv ) | grep FOO
131
132 (export FOO=BAR ; exec printenv ) | grep FOO
133 (FOO=BAR exec printenv ) | grep FOO
134
135 # ok, forget everything about hashed commands
136 hash -r
137 hash
138
139 # this had better succeed, since command -p guarantees we will find the
140 # standard utilities
141 command -p hash rm
142
143 # check out source/.
144
145 # sourcing a zero-length-file had better not be an error
146 rm -f /tmp/zero-length-file
147 cp /dev/null /tmp/zero-length-file
148 . /tmp/zero-length-file
149 echo $?
150 rm /tmp/zero-length-file
151
152 AVAR=AVAR
153
154 . ./source1.sub
155 AVAR=foo . ./source1.sub
156
157 . ./source2.sub
158 echo $?
159
160 set -- a b c
161 . ./source3.sub
162
163 # make sure source with arguments does not change the shell's positional
164 # parameters, but that the sourced file sees the arguments as its
165 # positional parameters
166 echo "$@"
167 . ./source3.sub x y z
168 echo "$@"
169
170 # but if the sourced script sets the positional parameters explicitly, they
171 # should be reflected in the calling shell's positional parameters. this
172 # also tests one of the shopt options that controls source using $PATH to
173 # find the script
174 echo "$@"
175 shopt -u sourcepath
176 . source4.sub
177 echo "$@"
178
179 # this is complicated when the sourced scripts gets its own positional
180 # parameters from arguments to `.'
181 set -- a b c
182 echo "$@"
183 . source4.sub x y z
184 echo "$@"
185
186 # test out cd and $CDPATH
187 ${THIS_SH} ./builtins1.sub
188
189 # test behavior of `.' when given a non-existent file argument
190 ${THIS_SH} ./source5.sub
191
192 # test bugs in sourcing non-regular files, fixed post-bash-3.2
193 ${THIS_SH} ./source6.sub
194
195 # test bugs with source called from multiline aliases and other contexts
196 ${THIS_SH} ./source7.sub
197
198 # in posix mode, assignment statements preceding special builtins are
199 # reflected in the shell environment. `.' and `eval' need special-case
200 # code.
201 set -o posix
202 echo $AVAR
203 AVAR=foo . ./source1.sub
204 echo $AVAR
205
206 AVAR=AVAR
207 echo $AVAR
208 AVAR=foo eval echo \$AVAR
209 echo $AVAR
210
211 AVAR=AVAR
212 echo $AVAR
213 AVAR=foo :
214 echo $AVAR
215 set +o posix
216
217 # but assignment statements preceding `export' are always reflected in
218 # the environment
219 foo="" export foo
220 declare -p foo
221 unset foo
222
223 # assignment statements preceding `declare' should be displayed correctly,
224 # but not persist after the command
225 FOO='$$' declare -p FOO
226 declare -p FOO
227 unset FOO
228
229 # except for `declare -x', which should be equivalent to `export'
230 FOO='$$' declare -x FOO
231 declare -p FOO
232 unset FOO
233
234 # test out kill -l. bash versions prior to 2.01 did `kill -l num' wrong
235 sigone=$(kill -l | sed -n 's:^ 1) *\([^ ]*\)[ ].*$:\1:p')
236
237 case "$(kill -l 1)" in
238 ${sigone/SIG/}) echo ok;;
239 *) echo oops -- kill -l failure;;
240 esac
241
242 # kill -l and trap -l should display exactly the same output
243 sigonea=$(trap -l | sed -n 's:^ 1) *\([^ ]*\)[ ].*$:\1:p')
244
245 if [ "$sigone" != "$sigonea" ]; then
246 echo oops -- kill -l and trap -l differ
247 fi
248
249 # POSIX.2 says that exit statuses > 128 are mapped to signal names by
250 # subtracting 128 so you can find out what signal killed a process
251 case "$(kill -l $(( 128 + 1)) )" in
252 ${sigone/SIG/}) echo ok;;
253 *) echo oops -- kill -l 129 failure;;
254 esac
255
256 # out-of-range signal numbers should report the argument in the error
257 # message, not 128 less than the argument
258 kill -l 4096
259
260 # kill -l NAME should return the signal number
261 kill -l ${sigone/SIG/}
262
263 # test behavior of shopt xpg_echo
264 ${THIS_SH} ./builtins2.sub
265
266 # test behavior of declare -g
267 ${THIS_SH} ./builtins3.sub
268
269 # test behavior of using declare to create variables without assigning values
270 ${THIS_SH} ./builtins4.sub
271
272 # test behavior of set and unset array variables
273 ${THIS_SH} ./builtins5.sub
274
275 # test behavior of unset builtin with -f and -v options
276 ${THIS_SH} ./builtins6.sub
277
278 # test behavior of command builtin after changing it to a pseudo-keyword
279 ${THIS_SH} ./builtins7.sub
280
281 # this must be last -- it is a fatal error
282 exit status
283
284 echo after bad exit