Imported from ../bash-2.01.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 # check that break breaks loops
8 for i in a b c; do echo $i; break; echo bad-$i; done
9 echo end-1
10 for i in a b c; do echo $i; break 1; echo bad-$i; done
11 echo end-2
12 for i in a b c; do
13         for j in x y z; do
14                 echo $i:$j
15                 break
16                 echo bad-$i
17         done
18         echo end-$i
19 done
20 echo end-3
21
22 # check that break breaks nested loops
23 for i in a b c; do
24         for j in x y z; do
25                 echo $i:$j
26                 break 2
27                 echo bad-$i
28         done
29         echo end-$i
30 done
31 echo end
32
33 # check that continue continues loops
34 for i in a b c; do echo $i; continue; echo bad-$i ; done
35 echo end-1
36 for i in a b c; do echo $i; continue 1; echo bad-$i; done
37 echo end-2
38 for i in a b c; do
39         for j in x y z; do
40                 echo $i:$j
41                 continue
42                 echo bad-$i-$j
43         done
44         echo end-$i
45 done
46 echo end-3
47
48 # check that continue breaks out of nested loops
49 for i in a b c; do
50         for j in x y z; do
51                 echo $i:$j
52                 continue 2
53                 echo bad-$i-$j
54         done
55         echo end-$i
56 done
57 echo end
58
59 # check that `eval' re-evaluates arguments, but `builtin' and `command' do not
60 AVAR='$BVAR'
61 BVAR=foo
62
63 echo $AVAR
64 builtin echo $AVAR
65 command echo $AVAR
66 eval echo \$AVAR
67 eval echo $AVAR
68
69 # test out eval with a temp environment
70 AVAR=bar eval echo \$AVAR
71 BVAR=xxx eval echo $AVAR
72
73 unset -v AVAR BVAR
74
75 # test umask
76 mask=$(umask)
77 umask 022
78 umask
79 umask -S
80 umask -S u=rwx,g=rwx,o=rx >/dev/null # 002
81 umask
82 umask -S
83 umask 0
84 umask -S
85 umask ${mask}   # restore original mask
86
87 # builtin/command without arguments should do nothing.  maybe someday they will
88 builtin
89 command
90
91 # test enable
92 enable -ps
93
94 enable -aps ; enable -nps
95
96 enable -n test
97 case "$(type -t test)" in
98 builtin)        echo oops -- enable -n test failed ;;
99 *)      echo enable -n test worked ;;
100 esac
101
102 enable test
103 case "$(type -t test)" in
104 builtin)        echo enable test worked ;;
105 *)      echo oops -- enable test failed ;;
106 esac
107
108 # test options to exec
109 (exec -a specialname ${THIS_SH} -c 'echo $0' )
110 # test `clean' environment.  if /bin/sh is bash, and the script version of
111 # printenv is run, there will be variables in the environment that bash
112 # sets on startup.
113 (export FOO=BAR ; exec -c printenv ) | grep FOO
114 (FOO=BAR exec -c printenv ) | grep FOO
115
116 (export FOO=BAR ; exec printenv ) | grep FOO
117 (FOO=BAR exec printenv ) | grep FOO
118
119 # ok, forget everything about hashed commands
120 hash -r
121 hash
122
123 # check out source/.
124
125 AVAR=AVAR
126
127 . ./source.sub1
128 AVAR=foo . ./source.sub1
129
130 . ./source.sub2
131 echo $?
132
133 set -- a b c
134 . ./source.sub3
135
136 # make sure source with arguments does not change the shell's positional
137 # parameters, but that the sourced file sees the arguments as its
138 # positional parameters
139 echo "$@"
140 . ./source.sub3 x y z
141 echo "$@"
142
143 # but if the sourced script sets the positional parameters explicitly, they
144 # should be reflected in the calling shell's positional parameters.  this
145 # also tests one of the shopt options that controls source using $PATH to
146 # find the script
147 echo "$@"
148 shopt -u sourcepath
149 . source.sub4
150 echo "$@"
151
152 # this is complicated when the sourced scripts gets its own positional
153 # parameters from arguments to `.'
154 set -- a b c
155 echo "$@"
156 . source.sub4 x y z
157 echo "$@"
158
159 # test out cd and $CDPATH
160 ${THIS_SH} ./builtins.sub1
161
162 # in posix mode, assignment statements preceding special builtins are
163 # reflected in the shell environment.  `.' and `eval' need special-case
164 # code.
165 set -o posix
166 echo $AVAR
167 AVAR=foo . ./source.sub1
168 echo $AVAR
169
170 AVAR=AVAR
171 echo $AVAR
172 AVAR=foo eval echo \$AVAR
173 echo $AVAR
174
175 AVAR=AVAR
176 echo $AVAR
177 AVAR=foo :
178 echo $AVAR
179
180 # test out kill -l.  bash versions prior to 2.01 did `kill -l num' wrong
181 set +o posix
182 sigone=$(kill -l | sed -n 's:^ 1) *\([^         ]*\)[   ].*$:\1:p')
183
184 case "$(kill -l 1)" in
185 ${sigone/SIG/}) echo ok;;
186 *)      echo oops -- kill -l failure;;
187 esac
188
189 # POSIX.2 says that exit statuses > 128 are mapped to signal names by
190 # subtracting 128 so you can find out what signal killed a process
191 case "$(kill -l $(( 128 + 1)) )" in
192 ${sigone/SIG/}) echo ok;;
193 *)      echo oops -- kill -l 129 failure;;
194 esac
195
196 # out-of-range signal numbers should report the argument in the error
197 # message, not 128 less than the argument
198 kill -l 4096
199
200 # kill -l NAME should return the signal number
201 kill -l ${sigone/SIG/}