3

My question is: I have a program written in C and to launch it I'm used to do

./program.exe var_1 ... var_n

where var_i-th are both numeric values and strings (used as filenames). I have to make the program run many times with different values of var_i-th and each time I have to call the above command.

Is there a way to prepare a file .txt (or something similar) of the kind

  • ./program.exe var_1 ... var_n #configuration one
  • .
  • .
  • ./program.exe var_1 ... var_n #configuration N

and to make the program run using this so that in a single call I get all the configurations run one by one?

Thanks

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
GRquanti
  • 527
  • 8
  • 23
  • Shell scripts will execute each line as though you had typed it in a console (more or less). So you should be able to just write all the commands in a file, then execute that--or are you looking for a way to have the script generate all the configuration permutations or something like that? – Eric Renouf May 07 '17 at 14:29
  • Not a C issue, therefore remove the C tag. – alk May 07 '17 at 14:30
  • Related http://stackoverflow.com/q/10929453/694576 – alk May 07 '17 at 14:31
  • Do you have a file containing all the parameters you need to run the program with? Or can they be generated easily - e.g. 1..100? – Mark Setchell May 07 '17 at 14:37
  • 1
    Keep in mind that the signature for the entry point to a C program is `int main(int argc, char** argv)`, so *all* your arguments are passed as C strings, no matter whether they're intended to be parsed to numbers or not. – Charles Duffy May 07 '17 at 14:46
  • The important question we need answered is whether your arguments need to be able to contain whitespace, and thus, whether quoting or escaping needs to be honored. If there's other content that needs expansion -- wildcards, for instance -- that needs to be covered as well. – Charles Duffy May 07 '17 at 14:48
  • @CharlesDuffy yes, I know. I was meaning the line would be someting like ./program.exe 1 2 3 file_one.dat file_two.dat file_three.dat Thanks however – GRquanti May 07 '17 at 15:38
  • The question I asked above -- which you haven't at yet answered -- is whether we only need to care about `file_one.dat` or also about `"file one.dat"`. – Charles Duffy May 07 '17 at 16:17
  • You need to care just about file_one.dat and not on "file_one.dat" However, the answer by Cyrus is working very well. – GRquanti May 15 '17 at 13:02

3 Answers3

3

To read each line into an array of strings, and pass that array to a program being run, can use methods described in BashFAQ #1:

while read -r -a args; do
  ./program.exe "${args[@]}"
done <in.txt

will read arguments from in.txt, expecting them to be whitespace-separated. It will not honor quotes or escape sequences as syntactically meaningful.


The caveat is that if your file contains:

arg1 "argument two" 13

you'll get:

./program.exe 'arg1' '"argument' 'two"' '13'

...for a total of four arguments.


If you need quotes to be honored, then we're back in the space of using xargs, but with one invocation per input line:

while read -r line; do
  xargs ./program.exe <<<"$line"
done <in.txt
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
3

Create a text file (foo.sh, e.g.) with this content, make it executable (chmod u+x foo.sh) and run it with ./foo.sh:

#/bin/bash

/program.exe var_1 ... var_1 #configuration one
/program.exe var_1 ... var_2 #configuration two
/program.exe var_1 ... var_3 #configuration three
.
.
.
./program.exe var_1 ... var_n #configuration N
Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

You may prepare a file with a customized parameter list per line, like this:

var_1
var_2
var_1 var_3

Then a bash sentence like this would do the work:

cat myFile.txt | xargs ./program.exe
JoseMiguel
  • 26
  • 2