在bash中连接字符串

Jas*_*son 5 bash shell

程序将输出设置为2位小数浮点数,文件中每行一个.根据执行情况,可以输出许多文件,每个文件的文件名为cancer.ex#,其中#是程序从脚本执行的次数.

教授提供了一个awk脚本作为使用gnuplot生成95%置信度图表的第一步.我想将输出转换为格式

conf $1 $2 $3 var#

其中#是来自cancer.ex的数字#

我开发了以下内容:

#!/bin/bash
Files=Output/*
String

for f in $Files 
do
    String="conf "
    cat $f | while read LINE
    do
        String="$LINE "
    done
echo $String
done
Run Code Online (Sandbox Code Playgroud)

我知道缺少一些步骤,因为我刚刚开始将它们放在一起.我的问题是执行连接部分,因为它根本不起作用.执行上面的脚本时没有输出,nada.但是,如果我String="$LINE改为echo $LINE,那么我将获得放在终端上的文件的所有输出.

bash中循环中的变量是否有可行的附加函数?

seh*_*ehe 7

#!/bin/bash
Files=( Output/* )
String

for f in "${Files[@]}"
do
    String="conf "
    while read LINE
    do
        String+="$LINE "
    done < "$f"
echo $String
done
Run Code Online (Sandbox Code Playgroud)

< "$f"管道的细微差别cat $f主要在于,while循环将在子shell中由于管道而执行,并且for循环中的变量实际上不会因子shell而更新.

另请注意,在各个方面我如何使文件名处理更加健壮(接受带空格的文件名,例如)

盒子外面?

这一切都说,我怀疑你可能只是完成

String="conf $(cat Output/*)"
#
String="$(for a in Output/*; do echo "conf $(cat "$a")"; done)"
Run Code Online (Sandbox Code Playgroud)

伪数据的概念证明:

mkdir Dummy
for a in {a..f}; do for b in {1..3}; do echo $a $b; done > Dummy/$a; done
for a in Dummy/*; do echo "conf " $(cat $a); done
Run Code Online (Sandbox Code Playgroud)

产量

conf  a 1 a 2 a 3
conf  b 1 b 2 b 3
conf  c 1 c 2 c 3
conf  d 1 d 2 d 3
conf  e 1 e 2 e 3
conf  f 1 f 2 f 3
Run Code Online (Sandbox Code Playgroud)