【发布时间】: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