【问题标题】:no newline in echo/printf in BASH while loopBASH while 循环中的 echo/printf 中没有换行符
【发布时间】:2014-04-18 15:14:03
【问题描述】:

为什么在执行以下脚本时,每个 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
}

现在,这会产生单行输出:

(entity:content member:)(entity:content name:id)(entity:content /name:)

如何将其更改为多行,例如:

(entity:content member:)
(entity:content name:id)
(entity:content /name:)

【问题讨论】:

  • printf 不是echo。你需要\n
  • printf "(entity:content %s:%s)\n" $ENTITY $CONTENT

标签: bash while-loop printf echo newline


【解决方案1】:

printf 不附加换行符作为标准行为,您需要将其添加到您的打印字符串中:

printf "(entity:content %s:%s)\n" $ENTITY $CONTENT

【讨论】:

    【解决方案2】:

    您只需在printf 语句中添加换行符\n

    printf "(entity:content %s:%s)\n" $ENTITY $CONTENT
    

    【讨论】:

      猜你喜欢
      • 2015-05-04
      • 1970-01-01
      • 2018-11-15
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 2015-02-15
      • 2010-11-20
      相关资源
      最近更新 更多