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