【问题标题】:Looping C compilation and running through BASH循环C编译并通过BASH运行
【发布时间】:2016-01-28 15:49:54
【问题描述】:

我正在尝试编译和运行 C 代码,同时通过 bash 循环输入文件。这是我用来自动化它的 bash 脚本的代码。

 ~!/bin/bash
 file=1
 outfile='outputnumber'$file
 readsfile='readsfilename'$file'.txt'
 compilefile=compiler$file'.o'
 gcc -lgsl -lgslcblas -std=c99 filewithccode.c -o $compilefile
 echo "Compilation over"
./$compilefile  $outfile $readsfile

所以我基本上想要做的是编译filewithcode.c,以便可执行文件存储为compiler1,它将outputnumber1和readsfilename1.txt作为输入。我想这样做的原因是,我可以在“文件”上循环它并为多个文件(我有 45 个)自动化它并自动执行。但我得到了错误:

Segmentation fault      (core dumped) ./$compilefile $outfile $readsfile

我试图为编译后的文件使用不同的名称,因为我试图在服务器上并行运行它们,我不确定使用相同的输出名称进行编译是否会导致问题。

有什么建议吗?我知道"./$" 可能会导致该错误,因为 BASH 正在回显“编译结束”。

【问题讨论】:

  • I am trying to use different names for the compiled file because I am trying to run them parallelly on a server and I am not sure if compiling with the same output name will cause an issue. 不,不会。编译一次并根据需要经常启动它,如果需要,每个都使用不同的命令行参数。 SIGSEGV 的原因很可能是在 C 代码中,您没有向我们展示。
  • 为什么不回显 ./$compilefile $outfile $readsfile 以确保一切运行正常,也可能是可执行文件本身的问题,您是否遇到过手动运行的问题?
  • 同意段错误来自你的 C。仅仅因为它编译并不意味着它不会出现段错误,特别是如果你使用 gcc 编译。您是否尝试过手动编译并在循环和 bash 脚本之外运行进行测试?
  • 为什么要在可执行输出文件的名称后面附加.o 扩展名?该扩展名通常用于目标文件,而不是可执行文件。
  • @tadamhicks 请解释“特别是如果你使用 gcc 编译”

标签: c linux bash parallel-processing


【解决方案1】:

您的最后一行,因为它只是放在一行上的变量,可能会导致您的脚本无法正确解释它。您可以尝试将其变成一个字符串,然后对该字符串使用 exec 命令。例如:

comm="./""$compilefile $outfile $readsfile"
exec $comm

这为我在过去引用变量时节省了很多语法问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 2019-09-03
    • 2023-03-27
    • 1970-01-01
    • 2017-03-19
    • 2011-08-07
    相关资源
    最近更新 更多