]>
Commit | Line | Data |
---|---|---|
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 | # | |
cce855bc | 14 | LC_ALL=C |
bb70624e | 15 | LC_NUMERIC=C |
cce855bc JA |
16 | |
17 | # these should output error messages -- the format is required | |
18 | printf | |
19 | printf -- | |
20 | ||
21 | # these should output nothing | |
22 | printf "" | |
23 | printf -- "" | |
24 | ||
95732b49 JA |
25 | # in the future this may mean to put the output into VAR, but for |
26 | # now it is an error | |
27 | # 2005-03-15 no longer an error | |
28 | unset var | |
29 | printf -v var "%10d" $RANDOM | |
30 | echo ${#var} | |
31 | ||
cce855bc JA |
32 | # this should expand escape sequences in the format string, nothing else |
33 | printf "\tone\n" | |
34 | ||
35 | # this should not cut off output after the \c | |
36 | printf "one\ctwo\n" | |
37 | ||
38 | # and unrecognized backslash escapes should have the backslash preserverd | |
39 | printf "4\.2\n" | |
40 | ||
41 | printf "no newline " ; printf "now newline\n" | |
42 | ||
43 | # %% -> % | |
44 | printf "%%\n" | |
45 | ||
bb70624e JA |
46 | # this was a bug caused by pre-processing the string for backslash escapes |
47 | # before doing the `%' format processing -- all versions before bash-2.04 | |
48 | printf "\045" ; echo | |
49 | printf "\045d\n" | |
50 | ||
cce855bc JA |
51 | # simple character output |
52 | printf "%c\n" ABCD | |
53 | ||
54 | # test simple string output | |
55 | printf "%s\n" unquoted | |
56 | ||
57 | # test quoted string output | |
58 | printf "%s %q\n" unquoted quoted | |
59 | printf "%s%10q\n" unquoted quoted | |
60 | ||
61 | printf "%q\n" 'this&that' | |
62 | ||
63 | # make sure the format string is reused to use up arguments | |
64 | printf "%d " 1 2 3 4 5; printf "\n" | |
65 | ||
66 | # make sure that extra format characters get null arguments | |
67 | printf "%s %d %d %d\n" onestring | |
68 | ||
69 | printf "%s %d %u %4.2f\n" onestring | |
70 | ||
71 | printf -- "--%s %s--\n" 4.2 '' | |
72 | printf -- "--%s %s--\n" 4.2 | |
73 | ||
74 | # test %b escapes | |
75 | ||
76 | # 8 is a non-octal digit, so the `81' should be output | |
77 | printf -- "--%b--\n" '\n\081' | |
78 | ||
79 | printf -- "--%b--\n" '\t\0101' | |
80 | printf -- "--%b--\n" '\t\101' | |
81 | ||
82 | # these should all display `A7' | |
0628567a | 83 | echo -e "\01017" |
f73dda09 | 84 | echo -e "\x417" |
cce855bc JA |
85 | |
86 | printf "%b\n" '\01017' | |
87 | printf "%b\n" '\1017' | |
f73dda09 | 88 | printf "%b\n" '\x417' |
cce855bc JA |
89 | |
90 | printf -- "--%b--\n" '\"abcd\"' | |
91 | printf -- "--%b--\n" "\'abcd\'" | |
92 | ||
93 | printf -- "--%b--\n" 'a\\x' | |
94 | ||
95 | printf -- "--%b--\n" '\x' | |
96 | ||
97 | Z1=$(printf -- "%b\n" '\a\b\e\f\r\v') | |
98 | Z2=$'\a\b\e\f\r\v' | |
99 | ||
100 | if [ "$Z1" != "$Z2" ]; then | |
101 | echo "whoops: printf %b and $'' differ" >&2 | |
102 | fi | |
103 | unset Z1 Z2 | |
104 | ||
105 | printf -- "--%b--\n" '' | |
106 | printf -- "--%b--\n" | |
107 | ||
108 | # the stuff following the \c should be ignored, as well as the rest | |
109 | # of the format string | |
110 | printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n" | |
111 | ||
bb70624e JA |
112 | # unrecognized escape sequences should by displayed unchanged |
113 | printf -- "--%b--\n" '4\.2' | |
114 | ||
115 | # a bare \ should not be processed as an escape sequence | |
116 | printf -- "--%b--\n" '\' | |
117 | ||
b72432fd JA |
118 | # make sure extra arguments are ignored if the format string doesn't |
119 | # actually use them | |
120 | printf "\n" 4.4 BSD | |
121 | printf " " 4.4 BSD ; printf "\n" | |
122 | ||
cce855bc JA |
123 | # make sure that a fieldwidth and precision of `*' are handled right |
124 | printf "%10.8s\n" 4.4BSD | |
125 | printf "%*.*s\n" 10 8 4.4BSD | |
126 | ||
127 | printf "%10.8q\n" 4.4BSD | |
128 | printf "%*.*q\n" 10 8 4.4BSD | |
129 | ||
130 | printf "%6b\n" 4.4BSD | |
131 | printf "%*b\n" 6 4.4BSD | |
132 | ||
133 | # we handle this crap with homemade code in printf.def | |
134 | printf "%10b\n" 4.4BSD | |
135 | printf -- "--%-10b--\n" 4.4BSD | |
136 | printf "%4.2b\n" 4.4BSD | |
137 | printf "%.3b\n" 4.4BSD | |
138 | printf -- "--%-8b--\n" 4.4BSD | |
139 | ||
140 | # test numeric conversions -- these four lines should echo identically | |
141 | printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255 | |
142 | printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255 | |
143 | ||
144 | printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255 | |
145 | printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255 | |
146 | ||
147 | printf "%10d\n" 42 | |
148 | printf "%10d\n" -42 | |
149 | ||
150 | printf "%*d\n" 10 42 | |
151 | printf "%*d\n" 10 -42 | |
152 | ||
153 | # test some simple floating point formats | |
154 | printf "%4.2f\n" 4.2 | |
155 | printf "%#4.2f\n" 4.2 | |
156 | printf "%#4.1f\n" 4.2 | |
157 | ||
158 | printf "%*.*f\n" 4 2 4.2 | |
159 | printf "%#*.*f\n" 4 2 4.2 | |
160 | printf "%#*.*f\n" 4 1 4.2 | |
161 | ||
162 | printf "%E\n" 4.2 | |
163 | printf "%e\n" 4.2 | |
164 | printf "%6.1E\n" 4.2 | |
165 | printf "%6.1e\n" 4.2 | |
166 | ||
167 | printf "%G\n" 4.2 | |
168 | printf "%g\n" 4.2 | |
169 | printf "%6.2G\n" 4.2 | |
170 | printf "%6.2g\n" 4.2 | |
171 | ||
172 | # test some of the more esoteric features of POSIX.1 printf | |
173 | printf "%d\n" "'string'" | |
174 | printf "%d\n" '"string"' | |
175 | ||
176 | printf "%#o\n" "'string'" | |
177 | printf "%#o\n" '"string"' | |
178 | ||
179 | printf "%#x\n" "'string'" | |
180 | printf "%#X\n" '"string"' | |
181 | ||
182 | printf "%6.2f\n" "'string'" | |
183 | printf "%6.2f\n" '"string"' | |
184 | ||
185 | # output from these two lines had better be the same | |
186 | printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz | |
187 | printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz | |
188 | ||
189 | # and these two also | |
190 | printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz | |
191 | printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz | |
192 | ||
bb70624e JA |
193 | # tests for translating \' to ' and \\ to \ |
194 | # printf translates \' to ' in the format string... | |
195 | printf "\'abcd\'\n" | |
196 | ||
197 | # but not when the %b format specification is used | |
198 | printf "%b\n" \\\'abcd\\\' | |
199 | ||
200 | # but both translate \\ to \ | |
201 | printf '\\abcd\\\n' | |
202 | printf "%b\n" '\\abcd\\' | |
203 | ||
204 | # this was reported as a bug in bash-2.03 | |
205 | # these three lines should all echo `26' | |
206 | printf "%d\n" 0x1a | |
207 | printf "%d\n" 032 | |
208 | printf "%d\n" 26 | |
209 | ||
cce855bc JA |
210 | # error messages |
211 | ||
212 | # this should be an overflow, but error messages vary between systems | |
213 | # printf "%lu\n" 4294967296 | |
214 | ||
215 | # ...but we cannot use this because some systems (SunOS4, for example), | |
216 | # happily ignore overflow conditions in strtol(3) | |
217 | #printf "%ld\n" 4294967296 | |
218 | ||
cce855bc JA |
219 | printf "%10" |
220 | printf "ab%Mcd\n" | |
221 | ||
222 | # this caused an infinite loop in older versions of printf | |
223 | printf "%y" 0 | |
224 | ||
f73dda09 | 225 | # these should print a warning and `0', according to POSIX.2 |
cce855bc JA |
226 | printf "%d\n" GNU |
227 | printf "%o\n" GNU | |
f73dda09 JA |
228 | |
229 | # failures in all bash versions through bash-2.05 | |
230 | printf "%.0s" foo | |
231 | printf "%.*s" 0 foo | |
232 | ||
233 | printf '%.0b-%.0s\n' foo bar | |
234 | printf '(%*b)(%*s)\n' -4 foo -4 bar | |
235 | ||
236 | format='%'`printf '%0100384d' 0`'d\n' | |
237 | printf $format 0 | |
238 | ||
95732b49 JA |
239 | # failures in all bash versions through bash-3.0 - undercounted characters |
240 | unset vv | |
241 | printf " %s %s %s \n%n" ab cd ef vv | |
242 | echo "$vv" | |
243 | ||
f73dda09 JA |
244 | # this doesn't work with printf(3) on all systems |
245 | #printf "%'s\n" foo | |
b80f6443 JA |
246 | |
247 | # test cases from an austin-group list discussion | |
248 | # prints ^G as an extension | |
249 | printf '%b\n' '\7' | |
250 | ||
251 | # prints ^G | |
252 | printf '%b\n' '\0007' | |
253 | ||
254 | # prints NUL then 7 | |
255 | printf '\0007\n' | |
256 | ||
257 | # prints no more than two hex digits | |
258 | printf '\x07e\n' | |
259 | ||
260 | # additional backslash escapes | |
261 | printf '\"\?\n' | |
0628567a JA |
262 | |
263 | # failures with decimal precisions until after bash-3.1 | |
264 | printf '%0.5d\n' 1 | |
265 | ||
266 | printf '%05d\n' 1 | |
267 | printf '%5d\n' 1 | |
268 | printf '%0d\n' 1 | |
3185942a JA |
269 | |
270 | # failures with various floating point formats and 0 after bash-3.2 | |
271 | ||
272 | printf "%G\n" 0 | |
273 | printf "%g\n" 0 | |
274 | printf "%4.2G\n" 0 | |
275 | printf "%4.2g\n" 0 | |
276 | ||
277 | printf "%G\n" 4 | |
278 | printf "%g\n" 4 | |
279 | printf "%4.2G\n" 4 | |
280 | printf "%4.2g\n" 4 | |
281 | ||
282 | printf "%F\n" 0 | |
283 | printf "%f\n" 0 | |
284 | printf "%4.2F\n" 0 | |
285 | printf "%4.2f\n" 0 | |
286 | ||
287 | printf "%F\n" 4 | |
288 | printf "%f\n" 4 | |
289 | printf "%4.2F\n" 4 | |
290 | printf "%4.2f\n" 4 | |
291 | ||
292 | printf "%E\n" 0 | |
293 | printf "%e\n" 0 | |
294 | printf "%4.2E\n" 0 | |
295 | printf "%4.2e\n" 0 | |
296 | ||
297 | printf "%E\n" 4 | |
298 | printf "%e\n" 4 | |
299 | printf "%4.2E\n" 4 | |
300 | printf "%4.2e\n" 4 | |
301 | ||
302 | printf "%08X\n" 2604292517 | |
303 | ||
304 | # make sure these format specifiers all output '' for empty string arguments | |
305 | echo q | |
306 | printf "%q\n" "" | |
307 | printf "%q\n" | |
308 | ||
309 | echo s | |
310 | printf "%s\n" '' | |
311 | printf "%s\n" | |
312 | ||
313 | echo b | |
314 | printf "%b\n" '' | |
315 | printf "%b\n" | |
316 | ||
317 | # bug in bash versions up to and including bash-3.2 | |
318 | v=yyy | |
319 | printf -v var "%s" '/current/working/directory/*.@(m3|i3|ig|mg)' | |
320 | shopt -s nullglob extglob | |
321 | echo "x$(printf "%b" @(hugo))x" | |
322 | printf -v var "%b" @(hugo); echo "x${var}x" | |
0001803f | 323 | |
d233b485 CR |
324 | # make sure that missing arguments are always handled like the empty string |
325 | printf "<%3s><%3b>\n" | |
326 | ||
495aee44 CR |
327 | # tests variable assignment with -v |
328 | ${THIS_SH} ./printf1.sub | |
329 | ||
0001803f | 330 | ${THIS_SH} ./printf2.sub |
495aee44 CR |
331 | |
332 | ${THIS_SH} ./printf3.sub | |
ac50fbac CR |
333 | |
334 | ${THIS_SH} ./printf4.sub |