Imported from ../bash-2.01.tar.gz.
[platform/upstream/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 # the iteration variable must be a valid identifier
14 for 1 in a b c; do echo $1; done
15
16 # try to rebind a read-only function
17 func()
18 {
19         echo func
20 }
21 readonly -f func
22 # make sure `readonly' and `declare' play well together
23 declare -Fr
24 func()
25 {
26         echo bar
27 }
28
29 # cannot unset readonly functions or variables
30 unset -f func
31 # or make them not readonly
32 declare -fr func
33 declare -f +r func
34
35 XPATH=$PATH
36 declare -r XPATH
37 unset -v XPATH
38
39 # cannot unset invalid identifiers
40 unset /bin/sh
41
42 # bad option
43 declare -z
44 # cannot declare invalid identifiers
45 declare -- -z 
46 declare /bin/sh
47
48 # this is the syntax used to export functions in the environment, but
49 # it cannot be used with `declare'
50 declare -f func='() { echo "this is func"; }'
51
52 # try to export -f something that is not a function -- this should be
53 # an error, not create an `invisible function'
54 export -f XPATH
55
56 # this depends on the setting of BREAK_COMPLAINS in config.h.in
57 break
58 continue
59
60 # this should not exit the shell; it did in versions before 2.01
61 shift label
62
63 # other shells do not complain about the extra arguments; maybe someday
64 # we won't either
65 set -- a b c
66 shift $# label
67 # and get rid of the positional parameters
68 shift $#
69
70 # let without an expression is an error, though maybe it should just return
71 # success
72 let
73
74 # local outside a function is an error
75 local
76
77 # try to hash a non-existant command
78 hash notthere
79
80 # turn off hashing, then try to hash something
81 set +o hashall
82 hash -p ${THIS_SH} ${THIS_SH##*/}
83
84 # bad identifiers to declare/readonly/export
85 export AA[4]
86 readonly AA[4]
87
88 declare -a AA
89 unset AA[-2]
90
91 # try to assign to a readonly array
92 declare -r AA
93 AA=( one two three )
94
95 # bad counts to `shift'
96 shopt -s shift_verbose
97 shift $(( $# + 5 ))
98 shift -2
99
100 # bad shell options
101 shopt -s no_such_option
102 shopt no_such_option
103
104 # non-octal digits for umask and other errors
105 umask 09
106 umask -S u=rwx:g=rwx:o=rx >/dev/null # 002
107 umask -S u:rwx,g:rwx,o:rx >/dev/null # 002
108 # this may behave identically to umask without arguments in the future,
109 # but for now it is an error
110 umask -p
111
112 # assignment to a readonly variable in environment
113 VAR=4
114 readonly VAR
115 VAR=7 :
116
117 # more readonly variable tests
118 declare VAR=88
119 declare +r VAR
120
121 declare -p unset
122
123 # iteration variable in a for statement being readonly
124 for VAR in 1 2 3 ; do echo $VAR; done
125
126 # parser errors
127 : $( for z in 1 2 3; do )
128 : $( for z in 1 2 3; done )
129
130 # various `cd' errors
131 ( unset HOME ; cd )
132 ( unset OLDPWD ; cd - )
133
134 # various `source/.' errors
135 .
136 source
137
138 # maybe someday this will work like in rc
139 . -i /dev/tty
140
141 # make sure that this gives an error rather than setting $1
142 set -q
143
144 # enable non-builtins
145 enable sh bash
146
147 # try to set and unset shell options simultaneously
148 shopt -s -u checkhash
149
150 # try to read into an invalid identifier
151 read /bin/sh < /dev/null
152
153 # try to read into a readonly variable
154 read VAR < /dev/null
155
156 # someday these may mean something, but for now they're errors
157 eval -i "echo $-"
158 command -i "echo $-"
159
160 # error to list trap for an unknown signal
161 trap -p NOSIG
162
163 # maybe someday trap will take a -s argument like kill, but not now
164 trap -p -s NOSIG
165
166 # maybe someday we will have a ksh-like ERR trap, but not yet
167 trap 'echo [$LINENO] -- error' ERR
168
169 # can only return from a function or sourced script
170 return 2
171
172 # break and continue with arguments <= 0
173 for z in 1 2 3; do
174         break 0
175         echo $x
176 done
177 for z in 1 2 3; do
178         continue 0
179         echo $x
180 done
181
182 # builtin with non-builtin
183 builtin bash
184
185 # maybe someday you will be able to use fg/bg when job control is not really
186 # active, but for now they are errors
187 bg
188 fg
189
190 # argument required
191 kill -s
192 # bad argument
193 kill -S
194
195 # this must be last!
196 # in posix mode, a function name must be a valid identifier
197 # this can't go in posix2.tests, since it causes the shell to exit
198 # immediately
199 set -o posix
200 function !! () { fc -s "$@" ; }
201 set +o posix
202
203 echo end