Kam*_*man 3 bash printf newline echo while-loop
为什么在执行以下脚本时,每个printf(也尝试与echo一起)打印在同一行上?
function read_dom () {
local IFS=\>
read -d \< ENTITY CONTENT
}
cat my_xml_file.xml | \
{ while read_dom; do
printf "(entity:content %s:%s)" $ENTITY $CONTENT
}
Run Code Online (Sandbox Code Playgroud)
现在,这将产生单行输出:
(entity:content member:)(entity:content name:id)(entity:content /name:)
Run Code Online (Sandbox Code Playgroud)
如何将其更改为多行,例如:
(entity:content member:)
(entity:content name:id)
(entity:content /name:)
Run Code Online (Sandbox Code Playgroud)
您只需要\n在printf语句中添加换行符即可:
printf "(entity:content %s:%s)\n" $ENTITY $CONTENT
Run Code Online (Sandbox Code Playgroud)
printf不附加换行符作为标准行为,您需要将其添加到打印字符串中:
printf "(entity:content %s:%s)\n" $ENTITY $CONTENT
Run Code Online (Sandbox Code Playgroud)