简体   繁体   English

Bash printf:如何理解零点s(“%0.s”)语法

[英]Bash printf: how to understand zero dot s (“%0.s”) syntax

Using Bash printf , I don't know how to understand the syntax of following code . 使用Bash printf ,我不知道如何理解以下代码的语法。

echo $(printf "%0.s../"  2  ); 
# ../
echo $(printf "%0.s../"  );   
# ../
echo $(printf "%0.s../" 1 );
# ../

reading from man printf , man printf阅读,

... ...

s 小号
Bytes from the string argument are printed until the end is reached or until the number of bytes indicated by the precision specification is reached; 将打印来自字符串参数的字节,直到达到末尾或达到精度规范指示的字节数为止; however if the precision is 0 or missing, the string is printed entirely. 但是,如果精度为0或缺少精度,则将完全打印字符串。

... ...

and I still don't know the code above. 而且我仍然不知道上面的代码。

and why the following code prints repeatedly? 为什么下面的代码反复打印?

# repeating character 
echo $(printf "%0.s../" 1 2  );
# ../../

seeing from the https://wiki.bash-hackers.org/commands/builtin/printf https://wiki.bash-hackers.org/commands/builtin/printf看到

... ...

. The dot: Together with a field width, the field is not expanded when the text is longer, the text is truncated instead. 点:加上字段宽度,当文本较长时,字段不会扩展,而是将文本截断。 "%.s" is an undocumented equivalent for "%.0s", which will force a field width of zero, effectively hiding the field from output “%.s”是未记录的等效于“%.0s”,它将强制字段宽度为零,从而有效地将字段隐藏在输出中

... ...

not got it yet. 还没有。 I need some explanation . 我需要一些解释。

This example may clarify how the width field works 此示例可以阐明width字段的工作原理

printf '%.0s../\n' ABCDEFG
../
printf '%.1s../\n' ABCDEFG
A../
printf '%.2s../\n' ABCDEFG
AB../
printf '%.7s../\n' ABCDEFG
ABCDEFG../

when the width is 0, then the field is not printed at all. 当宽度为0时,则根本不打印该字段。

Then, if there are more arguments than fields, printf repeats the format 然后,如果参数多于字段,则printf重复格式

printf '%.7s../\n' ABCDEFG abcdefg
ABCDEFG../
abcdefg../

What you have is classic problem of having fewer format specifiers than the actual arguments for the case of 你有什么是具有为的情况下,格式说明比实际参数少于的经典问题

printf "%0.s../" 1 2

Bash applies the specifier rules one-on-one with the arguments, but in a case of incorrect match, the specifier rules are basically copied as if its running as Bash与参数一对一地应用说明符规则,但是如果匹配不正确,说明符规则基本上会被复制,就像它以

printf "%0.s../../" 1

You need to add an extra specifier for 2 as 您需要为2添加一个额外的说明符

printf "%0.s..%0.s/" 1 2

in which case you just get a single ../ output as the formatting rules match. 在这种情况下,由于格式设置规则匹配,您只会得到一个../输出。

As for the syntax %0.s , it seems to be truncating the argument string provided as you've noted. 至于语法%0.s ,似乎已经截断了您所提供的参数字符串。 The only problem with your logic is the incorrect specifiers as explained above. 您的逻辑唯一的问题是上述错误的说明符。 The width field makes more sense when printing long values in the shell in which the precision with is controlled by the width fields. 在外壳中打印长值时,宽度字段更有意义,在外壳中,精确度由宽度字段控制。 Eg 例如

printf '%4.2f\n' 1323.343233
1323.34

which printf() understands as you have a number that comprise of 4 digits preceding the dot and a precision digits of maximum 2. 当您的数字由点前的4位数字和最多2的精度数字组成时, printf()理解。

You don't need to echo the output of printf() in the first-place. 您无需首先echoprintf()的输出。 You can use the format specifier \\n at the end to print the newline. 您可以在末尾使用格式说明符\\n来打印换行符。

printf "%0.s..%0.s/\n" 1 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM