Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command line parameters from a file

I have a C program that reads command line arguments from argv. Is it possible to make a pipe to redirect the contents of a file as command line arguments to my program? Suppose I have a file arguments.dat with this content:

0 0.2 302 0

And I want my program to be called with:

./myprogram 0 0.2 302 0

I tried the following:

cat arguments.dat | ./myprogram

without success.

like image 234
Jonatas Eduardo Avatar asked Jul 05 '11 15:07

Jonatas Eduardo


People also ask

How do I pass a command line argument in CMD?

For example, entering C:\abc.exe /W /F on a command line would run a program called abc.exe and pass two command line arguments to it: /W and /F.


1 Answers

xargs is your answer:

cat arguments.dat | xargs ./myprogram

Or easier:

xargs -a arguments.dat ./myprogram

Check the manual for the many ways to customize xargs. For example, you can read line-by-line rather than by word, and you can use the arguments in more complex replacements.

like image 72
Kerrek SB Avatar answered Nov 15 '22 07:11

Kerrek SB