]>
Commit | Line | Data |
---|---|---|
cce855bc | 1 | LC_ALL=C |
bb70624e | 2 | LC_NUMERIC=C |
cce855bc JA |
3 | |
4 | # these should output error messages -- the format is required | |
5 | printf | |
6 | printf -- | |
7 | ||
8 | # these should output nothing | |
9 | printf "" | |
10 | printf -- "" | |
11 | ||
95732b49 JA |
12 | # in the future this may mean to put the output into VAR, but for |
13 | # now it is an error | |
14 | # 2005-03-15 no longer an error | |
15 | unset var | |
16 | printf -v var "%10d" $RANDOM | |
17 | echo ${#var} | |
18 | ||
cce855bc JA |
19 | # this should expand escape sequences in the format string, nothing else |
20 | printf "\tone\n" | |
21 | ||
22 | # this should not cut off output after the \c | |
23 | printf "one\ctwo\n" | |
24 | ||
25 | # and unrecognized backslash escapes should have the backslash preserverd | |
26 | printf "4\.2\n" | |
27 | ||
28 | printf "no newline " ; printf "now newline\n" | |
29 | ||
30 | # %% -> % | |
31 | printf "%%\n" | |
32 | ||
bb70624e JA |
33 | # this was a bug caused by pre-processing the string for backslash escapes |
34 | # before doing the `%' format processing -- all versions before bash-2.04 | |
35 | printf "\045" ; echo | |
36 | printf "\045d\n" | |
37 | ||
cce855bc JA |
38 | # simple character output |
39 | printf "%c\n" ABCD | |
40 | ||
41 | # test simple string output | |
42 | printf "%s\n" unquoted | |
43 | ||
44 | # test quoted string output | |
45 | printf "%s %q\n" unquoted quoted | |
46 | printf "%s%10q\n" unquoted quoted | |
47 | ||
48 | printf "%q\n" 'this&that' | |
49 | ||
50 | # make sure the format string is reused to use up arguments | |
51 | printf "%d " 1 2 3 4 5; printf "\n" | |
52 | ||
53 | # make sure that extra format characters get null arguments | |
54 | printf "%s %d %d %d\n" onestring | |
55 | ||
56 | printf "%s %d %u %4.2f\n" onestring | |
57 | ||
58 | printf -- "--%s %s--\n" 4.2 '' | |
59 | printf -- "--%s %s--\n" 4.2 | |
60 | ||
61 | # test %b escapes | |
62 | ||
63 | # 8 is a non-octal digit, so the `81' should be output | |
64 | printf -- "--%b--\n" '\n\081' | |
65 | ||
66 | printf -- "--%b--\n" '\t\0101' | |
67 | printf -- "--%b--\n" '\t\101' | |
68 | ||
69 | # these should all display `A7' | |
70 | echo -e "\1017" | |
f73dda09 | 71 | echo -e "\x417" |
cce855bc JA |
72 | |
73 | printf "%b\n" '\01017' | |
74 | printf "%b\n" '\1017' | |
f73dda09 | 75 | printf "%b\n" '\x417' |
cce855bc JA |
76 | |
77 | printf -- "--%b--\n" '\"abcd\"' | |
78 | printf -- "--%b--\n" "\'abcd\'" | |
79 | ||
80 | printf -- "--%b--\n" 'a\\x' | |
81 | ||
82 | printf -- "--%b--\n" '\x' | |
83 | ||
84 | Z1=$(printf -- "%b\n" '\a\b\e\f\r\v') | |
85 | Z2=$'\a\b\e\f\r\v' | |
86 | ||
87 | if [ "$Z1" != "$Z2" ]; then | |
88 | echo "whoops: printf %b and $'' differ" >&2 | |
89 | fi | |
90 | unset Z1 Z2 | |
91 | ||
92 | printf -- "--%b--\n" '' | |
93 | printf -- "--%b--\n" | |
94 | ||
95 | # the stuff following the \c should be ignored, as well as the rest | |
96 | # of the format string | |
97 | printf -- "--%b--\n" '4.2\c5.4\n'; printf "\n" | |
98 | ||
bb70624e JA |
99 | # unrecognized escape sequences should by displayed unchanged |
100 | printf -- "--%b--\n" '4\.2' | |
101 | ||
102 | # a bare \ should not be processed as an escape sequence | |
103 | printf -- "--%b--\n" '\' | |
104 | ||
b72432fd JA |
105 | # make sure extra arguments are ignored if the format string doesn't |
106 | # actually use them | |
107 | printf "\n" 4.4 BSD | |
108 | printf " " 4.4 BSD ; printf "\n" | |
109 | ||
cce855bc JA |
110 | # make sure that a fieldwidth and precision of `*' are handled right |
111 | printf "%10.8s\n" 4.4BSD | |
112 | printf "%*.*s\n" 10 8 4.4BSD | |
113 | ||
114 | printf "%10.8q\n" 4.4BSD | |
115 | printf "%*.*q\n" 10 8 4.4BSD | |
116 | ||
117 | printf "%6b\n" 4.4BSD | |
118 | printf "%*b\n" 6 4.4BSD | |
119 | ||
120 | # we handle this crap with homemade code in printf.def | |
121 | printf "%10b\n" 4.4BSD | |
122 | printf -- "--%-10b--\n" 4.4BSD | |
123 | printf "%4.2b\n" 4.4BSD | |
124 | printf "%.3b\n" 4.4BSD | |
125 | printf -- "--%-8b--\n" 4.4BSD | |
126 | ||
127 | # test numeric conversions -- these four lines should echo identically | |
128 | printf "%d %u %i 0%o 0x%x 0x%X\n" 255 255 255 255 255 255 | |
129 | printf "%d %u %i %#o %#x %#X\n" 255 255 255 255 255 255 | |
130 | ||
131 | printf "%ld %lu %li 0%o 0x%x 0x%X\n" 255 255 255 255 255 255 | |
132 | printf "%ld %lu %li %#o %#x %#X\n" 255 255 255 255 255 255 | |
133 | ||
134 | printf "%10d\n" 42 | |
135 | printf "%10d\n" -42 | |
136 | ||
137 | printf "%*d\n" 10 42 | |
138 | printf "%*d\n" 10 -42 | |
139 | ||
140 | # test some simple floating point formats | |
141 | printf "%4.2f\n" 4.2 | |
142 | printf "%#4.2f\n" 4.2 | |
143 | printf "%#4.1f\n" 4.2 | |
144 | ||
145 | printf "%*.*f\n" 4 2 4.2 | |
146 | printf "%#*.*f\n" 4 2 4.2 | |
147 | printf "%#*.*f\n" 4 1 4.2 | |
148 | ||
149 | printf "%E\n" 4.2 | |
150 | printf "%e\n" 4.2 | |
151 | printf "%6.1E\n" 4.2 | |
152 | printf "%6.1e\n" 4.2 | |
153 | ||
154 | printf "%G\n" 4.2 | |
155 | printf "%g\n" 4.2 | |
156 | printf "%6.2G\n" 4.2 | |
157 | printf "%6.2g\n" 4.2 | |
158 | ||
159 | # test some of the more esoteric features of POSIX.1 printf | |
160 | printf "%d\n" "'string'" | |
161 | printf "%d\n" '"string"' | |
162 | ||
163 | printf "%#o\n" "'string'" | |
164 | printf "%#o\n" '"string"' | |
165 | ||
166 | printf "%#x\n" "'string'" | |
167 | printf "%#X\n" '"string"' | |
168 | ||
169 | printf "%6.2f\n" "'string'" | |
170 | printf "%6.2f\n" '"string"' | |
171 | ||
172 | # output from these two lines had better be the same | |
173 | printf -- "--%6.4s--\n" abcdefghijklmnopqrstuvwxyz | |
174 | printf -- "--%6.4b--\n" abcdefghijklmnopqrstuvwxyz | |
175 | ||
176 | # and these two also | |
177 | printf -- "--%12.10s--\n" abcdefghijklmnopqrstuvwxyz | |
178 | printf -- "--%12.10b--\n" abcdefghijklmnopqrstuvwxyz | |
179 | ||
bb70624e JA |
180 | # tests for translating \' to ' and \\ to \ |
181 | # printf translates \' to ' in the format string... | |
182 | printf "\'abcd\'\n" | |
183 | ||
184 | # but not when the %b format specification is used | |
185 | printf "%b\n" \\\'abcd\\\' | |
186 | ||
187 | # but both translate \\ to \ | |
188 | printf '\\abcd\\\n' | |
189 | printf "%b\n" '\\abcd\\' | |
190 | ||
191 | # this was reported as a bug in bash-2.03 | |
192 | # these three lines should all echo `26' | |
193 | printf "%d\n" 0x1a | |
194 | printf "%d\n" 032 | |
195 | printf "%d\n" 26 | |
196 | ||
cce855bc JA |
197 | # error messages |
198 | ||
199 | # this should be an overflow, but error messages vary between systems | |
200 | # printf "%lu\n" 4294967296 | |
201 | ||
202 | # ...but we cannot use this because some systems (SunOS4, for example), | |
203 | # happily ignore overflow conditions in strtol(3) | |
204 | #printf "%ld\n" 4294967296 | |
205 | ||
cce855bc JA |
206 | printf "%10" |
207 | printf "ab%Mcd\n" | |
208 | ||
209 | # this caused an infinite loop in older versions of printf | |
210 | printf "%y" 0 | |
211 | ||
f73dda09 | 212 | # these should print a warning and `0', according to POSIX.2 |
cce855bc JA |
213 | printf "%d\n" GNU |
214 | printf "%o\n" GNU | |
f73dda09 JA |
215 | |
216 | # failures in all bash versions through bash-2.05 | |
217 | printf "%.0s" foo | |
218 | printf "%.*s" 0 foo | |
219 | ||
220 | printf '%.0b-%.0s\n' foo bar | |
221 | printf '(%*b)(%*s)\n' -4 foo -4 bar | |
222 | ||
223 | format='%'`printf '%0100384d' 0`'d\n' | |
224 | printf $format 0 | |
225 | ||
95732b49 JA |
226 | # failures in all bash versions through bash-3.0 - undercounted characters |
227 | unset vv | |
228 | printf " %s %s %s \n%n" ab cd ef vv | |
229 | echo "$vv" | |
230 | ||
f73dda09 JA |
231 | # this doesn't work with printf(3) on all systems |
232 | #printf "%'s\n" foo | |
b80f6443 JA |
233 | |
234 | # test cases from an austin-group list discussion | |
235 | # prints ^G as an extension | |
236 | printf '%b\n' '\7' | |
237 | ||
238 | # prints ^G | |
239 | printf '%b\n' '\0007' | |
240 | ||
241 | # prints NUL then 7 | |
242 | printf '\0007\n' | |
243 | ||
244 | # prints no more than two hex digits | |
245 | printf '\x07e\n' | |
246 | ||
247 | # additional backslash escapes | |
248 | printf '\"\?\n' |