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