]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/errors.tests
Imported from ../bash-2.04.tar.gz.
[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
124readonly -a ZZZ=bbb
125
d166f048
JA
126# bad counts to `shift'
127shopt -s shift_verbose
128shift $(( $# + 5 ))
129shift -2
130
131# bad shell options
132shopt -s no_such_option
133shopt no_such_option
134
135# non-octal digits for umask and other errors
136umask 09
137umask -S u=rwx:g=rwx:o=rx >/dev/null # 002
138umask -S u:rwx,g:rwx,o:rx >/dev/null # 002
cce855bc
JA
139
140# at some point, this may mean `invert', but for now it is an error
141umask -i
d166f048 142
bb70624e
JA
143# bad assignments shouldn't change the umask
144mask=$(umask)
145umask g=u
146mask2=$(umask)
147if [ "$mask" != "$mask2" ]; then
148 echo "umask errors change process umask"
149fi
150
d166f048
JA
151# assignment to a readonly variable in environment
152VAR=4
153readonly VAR
154VAR=7 :
155
156# more readonly variable tests
157declare VAR=88
158declare +r VAR
159
160declare -p unset
161
162# iteration variable in a for statement being readonly
163for VAR in 1 2 3 ; do echo $VAR; done
164
165# parser errors
166: $( for z in 1 2 3; do )
167: $( for z in 1 2 3; done )
168
169# various `cd' errors
170( unset HOME ; cd )
cce855bc
JA
171( HOME=/tmp/xyz.bash ; cd )
172# errors from cd
173cd -
174cd /bin/sh # error - not a directory
175OLDPWD=/tmp/cd-notthere
176cd -
d166f048
JA
177
178# various `source/.' errors
179.
180source
181
182# maybe someday this will work like in rc
183. -i /dev/tty
184
185# make sure that this gives an error rather than setting $1
186set -q
187
188# enable non-builtins
189enable sh bash
190
191# try to set and unset shell options simultaneously
192shopt -s -u checkhash
193
bb70624e 194# this is an error -- bad timeout spec
cce855bc
JA
195read -t var < /dev/null
196
d166f048
JA
197# try to read into an invalid identifier
198read /bin/sh < /dev/null
199
200# try to read into a readonly variable
201read VAR < /dev/null
202
cce855bc
JA
203# bad option to readonly/export
204readonly -x foo
205
d166f048
JA
206# someday these may mean something, but for now they're errors
207eval -i "echo $-"
208command -i "echo $-"
209
cce855bc
JA
210# this caused a core dump in bash-2.01 (fixed in bash-2.01.1)
211eval echo \$[/bin/sh + 0]
212eval echo '$((/bin/sh + 0))'
213
d166f048
JA
214# error to list trap for an unknown signal
215trap -p NOSIG
216
217# maybe someday trap will take a -s argument like kill, but not now
218trap -p -s NOSIG
219
220# maybe someday we will have a ksh-like ERR trap, but not yet
221trap 'echo [$LINENO] -- error' ERR
222
223# can only return from a function or sourced script
224return 2
225
226# break and continue with arguments <= 0
227for z in 1 2 3; do
228 break 0
229 echo $x
230done
231for z in 1 2 3; do
232 continue 0
233 echo $x
234done
235
236# builtin with non-builtin
237builtin bash
238
239# maybe someday you will be able to use fg/bg when job control is not really
240# active, but for now they are errors
241bg
242fg
243
244# argument required
245kill -s
246# bad argument
247kill -S
cce855bc
JA
248# null argument
249kill -INT ''
250# argument required
251kill -INT
252
253# bad shell option names
254set -o trackall # bash is not ksh
d166f048
JA
255
256# this must be last!
257# in posix mode, a function name must be a valid identifier
258# this can't go in posix2.tests, since it causes the shell to exit
259# immediately
260set -o posix
261function !! () { fc -s "$@" ; }
262set +o posix
263
264echo end