【问题标题】:new line character in bashbash中的换行符
【发布时间】:2014-02-15 11:54:23
【问题描述】:

我目前正在编写一个 bash scipt,我需要在其中连接输出变量中的结果。但是我需要用换行符 '\n' 将它们分开,这似乎不起作用......有什么想法吗???

#!/bin/bash

for i in "./"*
do

#echo "$i"
tmp=$('/home/eclipseWorkspace/groundtruthPointExtractor/Debug/groundtruthPointExtractor' "./"$i) 
#echo $Output
#printf "$i $Output\n">> output.txt
Output=$Output$(printf $'\n %s %s' "$i" "$tmp" )
done
echo $Output
echo $Output> output.txt

【问题讨论】:

    标签: linux bash ubuntu newline


    【解决方案1】:

    您可以跳过在单个参数中累积输出

    DIR=/home/eclipseWorkspace/groundtruthPointExtractor/Debug
    for i in *; do
        printf "%s %s\n" "$i" "$("$DIR/groundtruthPointExtractor" "$i")"
    done | tee output.txt
    

    printf 在一行上输出文件名和程序输出。 for 循环中所有运行的聚合输出通过管道传输到tee,后者将输出写入指定文件和标准输出。

    【讨论】:

      【解决方案2】:

      看起来像

      echo "$str" 有效

      因为当你打印不带引号的字符串时,换行符会被转换为空格!!!

      【讨论】:

      【解决方案3】:

      echo 默认不解释反斜杠字符(如 \n)。使用:

      echo -e $Output
      
      
      -e     enable interpretation of backslash escapes
      

      【讨论】:

      • -e 在这里不相关。使用双引号:echo "$Output".
      • 如果没有引号,shell 将在调用 echo 之前对变量的值执行分词。
      猜你喜欢
      • 2011-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-10-19
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多