Skip to content
Advertisement

Looping C compilation and running through BASH

I’m trying to compile and run a C code while looping the input file through bash. Here is my code for the bash script I am using to automate it.

 ~!/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

So what I’m basically trying to do is compile filewithcode.c so that the executable is stored as compiler1, which takes outputnumber1 and readsfilename1.txt as input. The reason I want to do this is so that I can loop it over “file” and automate it for multiple files (I have 45 of them) and automate the execution. But I’m getting the error:

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

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.

Any suggestions? I know that maybe the "./$" is causing that error, because the BASH is echoing “Compilation over”.

Advertisement

Answer

Your last line, being that it’s just variables placed on a line might cause problems with your script being able to interpret it properly. You might try making it into a string, and then using the exec command on that string. for example:

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

This has saved me a lot of syntax trouble with referencing variables in the past.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement