]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/errors.tests
Bash-4.3 patch 7
[thirdparty/bash.git] / tests / errors.tests
CommitLineData
d166f048
JA
1# These should all be safe
2LC_ALL=C
3LC_CTYPE=C
4LC_COLLATE=C
5LC_MESSAGES=C
6
7# these tests should all generate errors
8
9# make sure we don't exit prematurely
10set +e
11set +o posix
12
cce855bc
JA
13# various alias/unalias errors
14
15# at some point, this may mean to `export' an alias, like ksh, but
16# for now it is an error
17alias -x foo=barz
18unalias -x fooaha
19alias hoowah
20unalias hoowah
21
d166f048
JA
22# the iteration variable must be a valid identifier
23for 1 in a b c; do echo $1; done
24
25# try to rebind a read-only function
26func()
27{
28 echo func
29}
30readonly -f func
31# make sure `readonly' and `declare' play well together
32declare -Fr
33func()
34{
35 echo bar
36}
37
cce855bc
JA
38# bad option
39unset -x func
40
d166f048
JA
41# cannot unset readonly functions or variables
42unset -f func
43# or make them not readonly
44declare -fr func
45declare -f +r func
46
47XPATH=$PATH
48declare -r XPATH
49unset -v XPATH
50
51# cannot unset invalid identifiers
52unset /bin/sh
53
cce855bc
JA
54# cannot unset function and variable at the same time
55unset -f -v SHELL
56
d166f048
JA
57# bad option
58declare -z
59# cannot declare invalid identifiers
60declare -- -z
61declare /bin/sh
62
63# this is the syntax used to export functions in the environment, but
64# it cannot be used with `declare'
65declare -f func='() { echo "this is func"; }'
66
cce855bc
JA
67# bad option to exec -- this should not exit the script
68exec -i /bin/sh
69
d166f048
JA
70# try to export -f something that is not a function -- this should be
71# an error, not create an `invisible function'
72export -f XPATH
73
74# this depends on the setting of BREAK_COMPLAINS in config.h.in
75break
76continue
77
78# this should not exit the shell; it did in versions before 2.01
79shift label
80
81# other shells do not complain about the extra arguments; maybe someday
82# we won't either
83set -- a b c
84shift $# label
85# and get rid of the positional parameters
86shift $#
87
88# let without an expression is an error, though maybe it should just return
89# success
90let
91
92# local outside a function is an error
93local
94
cce855bc
JA
95# logout of a non-login shell is an error
96logout
97
d166f048
JA
98# try to hash a non-existant command
99hash notthere
100
cce855bc
JA
101# bad option to hash, although it may mean `verbose' at some future point
102hash -v
103
d166f048
JA
104# turn off hashing, then try to hash something
105set +o hashall
106hash -p ${THIS_SH} ${THIS_SH##*/}
107
108# bad identifiers to declare/readonly/export
109export AA[4]
110readonly AA[4]
111
112declare -a AA
113unset AA[-2]
114
115# try to assign to a readonly array
116declare -r AA
117AA=( one two three )
118
cce855bc
JA
119# make sure `readonly -n' doesn't turn off readonly status
120readonly -n AA
121AA=(one two three)
122
123# try to assign a readonly array with bad assignment syntax
f73dda09
JA
124# NOTE: this works in post-bash-2.05 (at least when I write this)
125# readonly -a ZZZ=bbb
cce855bc 126
d166f048
JA
127# bad counts to `shift'
128shopt -s shift_verbose
129shift $(( $# + 5 ))
130shift -2
131
132# bad shell options
133shopt -s no_such_option
134shopt no_such_option
135
136# non-octal digits for umask and other errors
137umask 09
138umask -S u=rwx:g=rwx:o=rx >/dev/null # 002
139umask -S u:rwx,g:rwx,o:rx >/dev/null # 002
cce855bc
JA
140
141# at some point, this may mean `invert', but for now it is an error
142umask -i
d166f048 143
bb70624e
JA
144# bad assignments shouldn't change the umask
145mask=$(umask)
146umask g=u
147mask2=$(umask)
148if [ "$mask" != "$mask2" ]; then
149 echo "umask errors change process umask"
150fi
151
d166f048
JA
152# assignment to a readonly variable in environment
153VAR=4
154readonly VAR
155VAR=7 :
156
157# more readonly variable tests
158declare VAR=88
159declare +r VAR
160
161declare -p unset
162
163# iteration variable in a for statement being readonly
164for VAR in 1 2 3 ; do echo $VAR; done
165
166# parser errors
167: $( for z in 1 2 3; do )
168: $( for z in 1 2 3; done )
169
170# various `cd' errors
171( unset HOME ; cd )
cce855bc
JA
172( HOME=/tmp/xyz.bash ; cd )
173# errors from cd
174cd -
175cd /bin/sh # error - not a directory
176OLDPWD=/tmp/cd-notthere
177cd -
d166f048
JA
178
179# various `source/.' errors
180.
181source
182
183# maybe someday this will work like in rc
184. -i /dev/tty
185
186# make sure that this gives an error rather than setting $1
187set -q
188
189# enable non-builtins
190enable sh bash
191
192# try to set and unset shell options simultaneously
193shopt -s -u checkhash
194
bb70624e 195# this is an error -- bad timeout spec
cce855bc
JA
196read -t var < /dev/null
197
d166f048
JA
198# try to read into an invalid identifier
199read /bin/sh < /dev/null
200
201# try to read into a readonly variable
202read VAR < /dev/null
203
cce855bc
JA
204# bad option to readonly/export
205readonly -x foo
206
d166f048
JA
207# someday these may mean something, but for now they're errors
208eval -i "echo $-"
209command -i "echo $-"
210
cce855bc
JA
211# this caused a core dump in bash-2.01 (fixed in bash-2.01.1)
212eval echo \$[/bin/sh + 0]
213eval echo '$((/bin/sh + 0))'
214
d166f048
JA
215# error to list trap for an unknown signal
216trap -p NOSIG
217
218# maybe someday trap will take a -s argument like kill, but not now
219trap -p -s NOSIG
220
f73dda09
JA
221# we have a ksh-like ERR trap, post-bash-2.05
222#trap 'echo [$LINENO] -- error' ERR
d166f048
JA
223
224# can only return from a function or sourced script
225return 2
226
227# break and continue with arguments <= 0
228for z in 1 2 3; do
229 break 0
230 echo $x
231done
232for z in 1 2 3; do
233 continue 0
234 echo $x
235done
236
237# builtin with non-builtin
238builtin bash
239
240# maybe someday you will be able to use fg/bg when job control is not really
241# active, but for now they are errors
242bg
243fg
244
245# argument required
246kill -s
247# bad argument
248kill -S
cce855bc
JA
249# null argument
250kill -INT ''
251# argument required
252kill -INT
253
254# bad shell option names
255set -o trackall # bash is not ksh
d166f048 256
ac50fbac
CR
257# problem with versions through bash-4.2
258readonly xx=5
259echo $((xx=5))
260echo $?
261
262${THIS_SH} ./errors1.sub
263${THIS_SH} ./errors2.sub
264${THIS_SH} ./errors3.sub
265
d166f048
JA
266# this must be last!
267# in posix mode, a function name must be a valid identifier
268# this can't go in posix2.tests, since it causes the shell to exit
269# immediately
270set -o posix
271function !! () { fc -s "$@" ; }
272set +o posix
273
274echo end