djohoe28
djohoe28

Reputation: 45

How to call several command line arguments from a file in Bash?

I have a compiled C++ program which requires 2 command line arguments to run - for example, if my arguments are "10" & "3000", "Program" would be run as ./Program 10 3000

I want to read the command line arguments from a file called "args".

./Program args runs the program with 1 argument, args

./Program "$(< args)" where args = "10 3000" runs with 1 argument, 10 3000

and lastly, ./Program "$(< args1)" where args = "10{newline}3000" also runs with 1 argument, which is 10{newline}3000.

Is there any way to do this?

For the record, the idea is to use something along the lines of

./Program args1 < input1 > output1, ./Program args2 < input2 > output2, etc., so if there's any way to parametrize that as ./Program argsN < inputN > outputN and just call run(3) or something, I'd be happy to hear it :)

Note: C++'s cin is not to be used for this, only argc/argv.

Upvotes: 2

Views: 52

Answers (1)

Matias Barrios
Matias Barrios

Reputation: 5056

Lets say this is your cpp program :

#include <iostream>

using namespace std;
int main(int argc, char *argv[]){
    for(int i = 1; i < argc ; i++){
        cout << argv[i] << endl;
    }
}

And this is your input file :

10
30
Apples

Then you can do this :

./program $( < parameters.txt )

And the result would be :

 $ ./program $( < parameters.txt )
10
30
Apples

Hope it helps you!

Upvotes: 1

Related Questions