]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/printf1.sub
bash-5.2 distribution sources and documentation
[thirdparty/bash.git] / tests / printf1.sub
CommitLineData
8868edaf
CR
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#
495aee44
CR
14LC_ALL=C
15LC_NUMERIC=C
16
17unset vv
18
19# this should expand escape sequences in the format string, nothing else
20printf -v vv "\tone\n"
21printf "%s" "$vv"
22
23# this should not cut off output after the \c
24printf -v vv "one\ctwo\n"
25printf "%s" "$vv"
26
74091dd4 27# and unrecognized backslash escapes should have the backslash preserved
495aee44
CR
28printf -v vv "4\.2\n"
29printf "%s" "$vv"
30
31printf -v vv "no newline " ; printf "%s" "$vv" ; printf -v vv "now newline\n"
32printf "%s" "$vv"
33
34# %% -> %
35printf -v vv "%%\n"
36printf "%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
40printf -v vv "\045"
41printf "%s" "$vv"
42echo
43printf -v vv "\045d\n"
44printf "%s" "$vv"
45
46# simple character output
47printf -v vv "%c\n" ABCD
48printf "%s" "$vv"
49
50# test simple string output
51printf -v vv "%s\n" unquoted
52printf "%s" "$vv"
53
54# test quoted string output
55printf -v vv "%s %q\n" unquoted quoted
56printf "%s" "$vv"
57printf -v vv "%s%10q\n" unquoted quoted
58printf "%s" "$vv"
59
60printf -v vv "%q\n" 'this&that'
61printf "%s" "$vv"
62
63# make sure the format string is reused to use up arguments
64printf -v vv "%d " 1 2 3 4 5
65printf "%s" "$vv"
66echo
67
68# make sure that extra format characters get null arguments
69printf -v vv "%s %d %d %d\n" onestring
70printf "%s" "$vv"
71
72printf -v vv "%s %d %u %4.2f\n" onestring
73printf "%s" "$vv"
74
75printf -v vv -- "--%s %s--\n" 4.2 ''
76printf "%s" "$vv"
77printf -v vv -- "--%s %s--\n" 4.2
78printf "%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
86printf -v vv -- "--%b--\n" '\t\0101'
87printf "%s" "$vv"
88printf -v vv -- "--%b--\n" '\t\101'
89printf "%s" "$vv"
90
91# these should all display `A7'
92printf -v vv "%b\n" '\01017'
93printf "%s" "$vv"
94printf -v vv "%b\n" '\1017'
95printf "%s" "$vv"
96printf -v vv "%b\n" '\x417'
97printf "%s" "$vv"
98
99printf -v vv -- "--%b--\n" '\"abcd\"'
100printf "%s" "$vv"
101printf -v vv -- "--%b--\n" "\'abcd\'"
102printf "%s" "$vv"
103
104printf -v vv -- "--%b--\n" 'a\\x'
105printf "%s" "$vv"
106
107printf -v vv -- "--%b--\n" '\x'
108printf "%s" "$vv"
109
110Z1=$(printf -- "%b\n" '\a\b\e\f\r\v')
111Z2=$'\a\b\e\f\r\v'
112
113if [ "$Z1" != "$Z2" ]; then
114 printf "%s" "whoops: printf -v vv %b and $'' differ" >&2
115fi
116unset Z1 Z2
117
118printf -v vv -- "--%b--\n" ''
119printf "%s" "$vv"
120printf -v vv -- "--%b--\n"
121printf "%s" "$vv"
122
123# the stuff following the \c should be ignored, as well as the rest
124# of the format string
125printf -v vv -- "--%b--\n" '4.2\c5.4\n'
126printf "%s" "$vv"
127echo
128
129# unrecognized escape sequences should by displayed unchanged
130printf -v vv -- "--%b--\n" '4\.2'
131printf "%s" "$vv"
132
133# a bare \ should not be processed as an escape sequence
134printf -v vv -- "--%b--\n" '\'
135printf "%s" "$vv"
136
137# make sure extra arguments are ignored if the format string doesn't
138# actually use them
139printf -v vv "\n" 4.4 BSD
140printf "%s" "$vv"
141printf -v vv " " 4.4 BSD
142printf "%s" "$vv"
143echo
144
145# make sure that a fieldwidth and precision of `*' are handled right
146printf -v vv "%10.8s\n" 4.4BSD
147printf "%s" "$vv"
148printf -v vv "%*.*s\n" 10 8 4.4BSD
149printf "%s" "$vv"
150
151printf -v vv "%10.8q\n" 4.4BSD
152printf "%s" "$vv"
153printf -v vv "%*.*q\n" 10 8 4.4BSD
154printf "%s" "$vv"
155
156printf -v vv "%6b\n" 4.4BSD
157printf "%s" "$vv"
158printf -v vv "%*b\n" 6 4.4BSD
159printf "%s" "$vv"
160
161# we handle this crap with homemade code in printf -v vv.def
162printf -v vv "%10b\n" 4.4BSD
163printf "%s" "$vv"
164printf -v vv -- "--%-10b--\n" 4.4BSD
165printf "%s" "$vv"
166printf -v vv "%4.2b\n" 4.4BSD
167printf "%s" "$vv"
168printf -v vv "%.3b\n" 4.4BSD
169printf "%s" "$vv"
170printf -v vv -- "--%-8b--\n" 4.4BSD
171printf "%s" "$vv"
172
173# test numeric conversions -- these four lines should printf "%s" identically
174printf -v vv "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
175printf "%s" "$vv"
176printf -v vv "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255
177printf "%s" "$vv"
178
179printf -v vv "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255
180printf "%s" "$vv"
181printf -v vv "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255
182printf "%s" "$vv"
183
184printf -v vv "%10d\n" 42
185printf "%s" "$vv"
186printf -v vv "%10d\n" -42
187printf "%s" "$vv"
188
189printf -v vv "%*d\n" 10 42
190printf "%s" "$vv"
191printf -v vv "%*d\n" 10 -42
192printf "%s" "$vv"
193
194# test some simple floating point formats
195printf -v vv "%4.2f\n" 4.2
196printf "%s" "$vv"
197printf -v vv "%#4.2f\n" 4.2
198printf "%s" "$vv"
199printf -v vv "%#4.1f\n" 4.2
200printf "%s" "$vv"
201
202printf -v vv "%*.*f\n" 4 2 4.2
203printf "%s" "$vv"
204printf -v vv "%#*.*f\n" 4 2 4.2
205printf "%s" "$vv"
206printf -v vv "%#*.*f\n" 4 1 4.2
207printf "%s" "$vv"
208
209printf -v vv "%E\n" 4.2
210printf "%s" "$vv"
211printf -v vv "%e\n" 4.2
212printf "%s" "$vv"
213printf -v vv "%6.1E\n" 4.2
214printf "%s" "$vv"
215printf -v vv "%6.1e\n" 4.2
216printf "%s" "$vv"
217
218printf -v vv "%G\n" 4.2
219printf "%s" "$vv"
220printf -v vv "%g\n" 4.2
221printf "%s" "$vv"
222printf -v vv "%6.2G\n" 4.2
223printf "%s" "$vv"
224printf -v vv "%6.2g\n" 4.2
225printf "%s" "$vv"
226
227# test some of the more esoteric features of POSIX.1 printf -v vv
228printf -v vv "%d\n" "'string'"
229printf "%s" "$vv"
230printf -v vv "%d\n" '"string"'
231printf "%s" "$vv"
232
233printf -v vv "%#o\n" "'string'"
234printf "%s" "$vv"
235printf -v vv "%#o\n" '"string"'
236printf "%s" "$vv"
237
238printf -v vv "%#x\n" "'string'"
239printf "%s" "$vv"
240printf -v vv "%#X\n" '"string"'
241printf "%s" "$vv"
242
243printf -v vv "%6.2f\n" "'string'"
244printf "%s" "$vv"
245printf -v vv "%6.2f\n" '"string"'
246printf "%s" "$vv"
247
248# output from these two lines had better be the same
249printf -v vv -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz
250printf "%s" "$vv"
251printf -v vv -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz
252printf "%s" "$vv"
253
254# and these two also
255printf -v vv -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz
256printf "%s" "$vv"
257printf -v vv -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz
258printf "%s" "$vv"
259
260# tests for translating \' to ' and \\ to \
261# printf -v vv translates \' to ' in the format string...
262printf -v vv "\'abcd\'\n"
263printf "%s" "$vv"
264
265# but not when the %b format specification is used
266printf -v vv "%b\n" \\\'abcd\\\'
267printf "%s" "$vv"
268
269# but both translate \\ to \
270printf -v vv '\\abcd\\\n'
271printf "%s" "$vv"
272printf -v vv "%b\n" '\\abcd\\'
273printf "%s" "$vv"
274
275# this was reported as a bug in bash-2.03
276# these three lines should all printf "%s" `26'
277printf -v vv "%d\n" 0x1a
278printf "%s" "$vv"
279printf -v vv "%d\n" 032
280printf "%s" "$vv"
281printf -v vv "%d\n" 26
282printf "%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
293printf -v vv "%10"
294printf -v vv "ab%Mcd\n"
295
296# this caused an infinite loop in older versions of printf -v vv
297printf -v vv "%y" 0
298
299# these should print a warning and `0', according to POSIX.2
300printf -v vv "%d\n" GNU
301printf "%s" "$vv"
302printf -v vv "%o\n" GNU
303printf "%s" "$vv"
304
305# failures in all bash versions through bash-2.05
306printf -v vv "%.0s" foo
307printf "%s" "$vv"
308printf -v vv "%.*s" 0 foo
309printf "%s" "$vv"
310
311printf -v vv '%.0b-%.0s\n' foo bar
312printf "%s" "$vv"
313printf -v vv '(%*b)(%*s)\n' -4 foo -4 bar
314printf "%s" "$vv"
315
316format='%'`printf '%0100384d' 0`'d\n'
317printf -v vv $format 0
318printf "%s" "$vv"
319
320# failures in all bash versions through bash-3.0 - undercounted characters
321unset vv
322printf -v vv " %s %s %s \n%n" ab cd ef vvv
323printf "%s" "$vv"
324echo $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
331printf -v vv '%b\n' '\7'
332printf "%s" "$vv"
333
334# prints ^G
335printf -v vv '%b\n' '\0007'
336printf "%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
343printf -v vv '\x07e\n'
344printf "%s" "$vv"
345
346# additional backslash escapes
347printf -v vv '\"\?\n'
348printf "%s" "$vv"