]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/printf1.sub
bash-5.1 distribution sources and documentation
[thirdparty/bash.git] / tests / printf1.sub
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 #
14 LC_ALL=C
15 LC_NUMERIC=C
16
17 unset vv
18
19 # this should expand escape sequences in the format string, nothing else
20 printf -v vv "\tone\n"
21 printf "%s" "$vv"
22
23 # this should not cut off output after the \c
24 printf -v vv "one\ctwo\n"
25 printf "%s" "$vv"
26
27 # and unrecognized backslash escapes should have the backslash preserverd
28 printf -v vv "4\.2\n"
29 printf "%s" "$vv"
30
31 printf -v vv "no newline " ; printf "%s" "$vv" ; printf -v vv "now newline\n"
32 printf "%s" "$vv"
33
34 # %% -> %
35 printf -v vv "%%\n"
36 printf "%s" "$vv"
37
38 # this was a bug caused by pre-processing the string for backslash escapes
39 # before doing the `%' format processing -- all versions before bash-2.04
40 printf -v vv "\045"
41 printf "%s" "$vv"
42 echo
43 printf -v vv "\045d\n"
44 printf "%s" "$vv"
45
46 # simple character output
47 printf -v vv "%c\n" ABCD
48 printf "%s" "$vv"
49
50 # test simple string output
51 printf -v vv "%s\n" unquoted
52 printf "%s" "$vv"
53
54 # test quoted string output
55 printf -v vv "%s %q\n" unquoted quoted
56 printf "%s" "$vv"
57 printf -v vv "%s%10q\n" unquoted quoted
58 printf "%s" "$vv"
59
60 printf -v vv "%q\n" 'this&that'
61 printf "%s" "$vv"
62
63 # make sure the format string is reused to use up arguments
64 printf -v vv "%d " 1 2 3 4 5
65 printf "%s" "$vv"
66 echo
67
68 # make sure that extra format characters get null arguments
69 printf -v vv "%s %d %d %d\n" onestring
70 printf "%s" "$vv"
71
72 printf -v vv "%s %d %u %4.2f\n" onestring
73 printf "%s" "$vv"
74
75 printf -v vv -- "--%s %s--\n" 4.2 ''
76 printf "%s" "$vv"
77 printf -v vv -- "--%s %s--\n" 4.2
78 printf "%s" "$vv"
79
80 # test %b escapes
81
82 # 8 is a non-octal digit, so the `81' should be output
83 #printf -v vv -- "--%b--\n" '\n\081'
84 #printf "%s" "$vv"
85
86 printf -v vv -- "--%b--\n" '\t\0101'
87 printf "%s" "$vv"
88 printf -v vv -- "--%b--\n" '\t\101'
89 printf "%s" "$vv"
90
91 # these should all display `A7'
92 printf -v vv "%b\n" '\01017'
93 printf "%s" "$vv"
94 printf -v vv "%b\n" '\1017'
95 printf "%s" "$vv"
96 printf -v vv "%b\n" '\x417'
97 printf "%s" "$vv"
98
99 printf -v vv -- "--%b--\n" '\"abcd\"'
100 printf "%s" "$vv"
101 printf -v vv -- "--%b--\n" "\'abcd\'"
102 printf "%s" "$vv"
103
104 printf -v vv -- "--%b--\n" 'a\\x'
105 printf "%s" "$vv"
106
107 printf -v vv -- "--%b--\n" '\x'
108 printf "%s" "$vv"
109
110 Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
111 Z2=$'\a\b\e\f\r\v'
112
113 if [ "$Z1" != "$Z2" ]; then
114 printf "%s" "whoops: printf -v vv %b and $'' differ" >&2
115 fi
116 unset Z1 Z2
117
118 printf -v vv -- "--%b--\n" ''
119 printf "%s" "$vv"
120 printf -v vv -- "--%b--\n"
121 printf "%s" "$vv"
122
123 # the stuff following the \c should be ignored, as well as the rest
124 # of the format string
125 printf -v vv -- "--%b--\n" '4.2\c5.4\n'
126 printf "%s" "$vv"
127 echo
128
129 # unrecognized escape sequences should by displayed unchanged
130 printf -v vv -- "--%b--\n" '4\.2'
131 printf "%s" "$vv"
132
133 # a bare \ should not be processed as an escape sequence
134 printf -v vv -- "--%b--\n" '\'
135 printf "%s" "$vv"
136
137 # make sure extra arguments are ignored if the format string doesn't
138 # actually use them
139 printf -v vv "\n" 4.4 BSD
140 printf "%s" "$vv"
141 printf -v vv " " 4.4 BSD
142 printf "%s" "$vv"
143 echo
144
145 # make sure that a fieldwidth and precision of `*' are handled right
146 printf -v vv "%10.8s\n" 4.4BSD
147 printf "%s" "$vv"
148 printf -v vv "%*.*s\n" 10 8 4.4BSD
149 printf "%s" "$vv"
150
151 printf -v vv "%10.8q\n" 4.4BSD
152 printf "%s" "$vv"
153 printf -v vv "%*.*q\n" 10 8 4.4BSD
154 printf "%s" "$vv"
155
156 printf -v vv "%6b\n" 4.4BSD
157 printf "%s" "$vv"
158 printf -v vv "%*b\n" 6 4.4BSD
159 printf "%s" "$vv"
160
161 # we handle this crap with homemade code in printf -v vv.def
162 printf -v vv "%10b\n" 4.4BSD
163 printf "%s" "$vv"
164 printf -v vv -- "--%-10b--\n" 4.4BSD
165 printf "%s" "$vv"
166 printf -v vv "%4.2b\n" 4.4BSD
167 printf "%s" "$vv"
168 printf -v vv "%.3b\n" 4.4BSD
169 printf "%s" "$vv"
170 printf -v vv -- "--%-8b--\n" 4.4BSD
171 printf "%s" "$vv"
172
173 # test numeric conversions -- these four lines should printf "%s" identically
174 printf -v vv "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
175 printf "%s" "$vv"
176 printf -v vv "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
177 printf "%s" "$vv"
178
179 printf -v vv "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
180 printf "%s" "$vv"
181 printf -v vv "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
182 printf "%s" "$vv"
183
184 printf -v vv "%10d\n" 42
185 printf "%s" "$vv"
186 printf -v vv "%10d\n" -42
187 printf "%s" "$vv"
188
189 printf -v vv "%*d\n" 10 42
190 printf "%s" "$vv"
191 printf -v vv "%*d\n" 10 -42
192 printf "%s" "$vv"
193
194 # test some simple floating point formats
195 printf -v vv "%4.2f\n" 4.2
196 printf "%s" "$vv"
197 printf -v vv "%#4.2f\n" 4.2
198 printf "%s" "$vv"
199 printf -v vv "%#4.1f\n" 4.2
200 printf "%s" "$vv"
201
202 printf -v vv "%*.*f\n" 4 2 4.2
203 printf "%s" "$vv"
204 printf -v vv "%#*.*f\n" 4 2 4.2
205 printf "%s" "$vv"
206 printf -v vv "%#*.*f\n" 4 1 4.2
207 printf "%s" "$vv"
208
209 printf -v vv "%E\n" 4.2
210 printf "%s" "$vv"
211 printf -v vv "%e\n" 4.2
212 printf "%s" "$vv"
213 printf -v vv "%6.1E\n" 4.2
214 printf "%s" "$vv"
215 printf -v vv "%6.1e\n" 4.2
216 printf "%s" "$vv"
217
218 printf -v vv "%G\n" 4.2
219 printf "%s" "$vv"
220 printf -v vv "%g\n" 4.2
221 printf "%s" "$vv"
222 printf -v vv "%6.2G\n" 4.2
223 printf "%s" "$vv"
224 printf -v vv "%6.2g\n" 4.2
225 printf "%s" "$vv"
226
227 # test some of the more esoteric features of POSIX.1 printf -v vv
228 printf -v vv "%d\n" "'string'"
229 printf "%s" "$vv"
230 printf -v vv "%d\n" '"string"'
231 printf "%s" "$vv"
232
233 printf -v vv "%#o\n" "'string'"
234 printf "%s" "$vv"
235 printf -v vv "%#o\n" '"string"'
236 printf "%s" "$vv"
237
238 printf -v vv "%#x\n" "'string'"
239 printf "%s" "$vv"
240 printf -v vv "%#X\n" '"string"'
241 printf "%s" "$vv"
242
243 printf -v vv "%6.2f\n" "'string'"
244 printf "%s" "$vv"
245 printf -v vv "%6.2f\n" '"string"'
246 printf "%s" "$vv"
247
248 # output from these two lines had better be the same
249 printf -v vv -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
250 printf "%s" "$vv"
251 printf -v vv -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
252 printf "%s" "$vv"
253
254 # and these two also
255 printf -v vv -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
256 printf "%s" "$vv"
257 printf -v vv -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
258 printf "%s" "$vv"
259
260 # tests for translating \' to ' and \\ to \
261 # printf -v vv translates \' to ' in the format string...
262 printf -v vv "\'abcd\'\n"
263 printf "%s" "$vv"
264
265 # but not when the %b format specification is used
266 printf -v vv "%b\n" \\\'abcd\\\'
267 printf "%s" "$vv"
268
269 # but both translate \\ to \
270 printf -v vv '\\abcd\\\n'
271 printf "%s" "$vv"
272 printf -v vv "%b\n" '\\abcd\\'
273 printf "%s" "$vv"
274
275 # this was reported as a bug in bash-2.03
276 # these three lines should all printf "%s" `26'
277 printf -v vv "%d\n" 0x1a
278 printf "%s" "$vv"
279 printf -v vv "%d\n" 032
280 printf "%s" "$vv"
281 printf -v vv "%d\n" 26
282 printf "%s" "$vv"
283
284 # error messages
285
286 # this should be an overflow, but error messages vary between systems
287 # printf -v vv "%lu\n" 4294967296
288
289 # ...but we cannot use this because some systems (SunOS4, for example),
290 # happily ignore overflow conditions in strtol(3)
291 #printf -v vv "%ld\n" 4294967296
292
293 printf -v vv "%10"
294 printf -v vv "ab%Mcd\n"
295
296 # this caused an infinite loop in older versions of printf -v vv
297 printf -v vv "%y" 0
298
299 # these should print a warning and `0', according to POSIX.2
300 printf -v vv "%d\n" GNU
301 printf "%s" "$vv"
302 printf -v vv "%o\n" GNU
303 printf "%s" "$vv"
304
305 # failures in all bash versions through bash-2.05
306 printf -v vv "%.0s" foo
307 printf "%s" "$vv"
308 printf -v vv "%.*s" 0 foo
309 printf "%s" "$vv"
310
311 printf -v vv '%.0b-%.0s\n' foo bar
312 printf "%s" "$vv"
313 printf -v vv '(%*b)(%*s)\n' -4 foo -4 bar
314 printf "%s" "$vv"
315
316 format='%'`printf '%0100384d' 0`'d\n'
317 printf -v vv $format 0
318 printf "%s" "$vv"
319
320 # failures in all bash versions through bash-3.0 - undercounted characters
321 unset vv
322 printf -v vv " %s %s %s \n%n" ab cd ef vvv
323 printf "%s" "$vv"
324 echo $vvv
325
326 # this doesn't work with printf -v vv(3) on all systems
327 #printf -v vv "%'s\n" foo
328
329 # test cases from an austin-group list discussion
330 # prints ^G as an extension
331 printf -v vv '%b\n' '\7'
332 printf "%s" "$vv"
333
334 # prints ^G
335 printf -v vv '%b\n' '\0007'
336 printf "%s" "$vv"
337
338 # prints NUL then 7
339 #printf -v vv '\0007\n'
340 #printf "%s" "$vv"
341
342 # prints no more than two hex digits
343 printf -v vv '\x07e\n'
344 printf "%s" "$vv"
345
346 # additional backslash escapes
347 printf -v vv '\"\?\n'
348 printf "%s" "$vv"