Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use file contents as command-line arguments?

Tags:

linux

bash

I'd like to use arguments from file as command-line arguments for some commands like gcc or ls.

For example gcc -o output -Wall -Werro

as file consist of:

-o output -Wall -Werro

Used for gcc command-line call.

like image 931
morfis Avatar asked Mar 24 '10 21:03

morfis


People also ask

How do you pass values into command line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

How do I view the contents of a file in Terminal?

To look at the contents of a text-based configuration file, use cat or less . Generally, you'll use less because it has more options (such as searching). To use less , enter the command name followed by the name of the file you want to view. The first page of text fills the window.

How do you use command line arguments?

A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.


3 Answers

Some programs use the "@" semantics to feed in args from a file eg. gcc @argfile

Where, for gcc, argfile contains options

-ansi
-I/usr/include/mylib

This can be nested so that argfile can contain

-ansi
-I/usr/include/mylib
@argfile2

http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Overall-Options.html#Overall-Options

like image 129
cartland Avatar answered Sep 19 '22 19:09

cartland


You can use xargs:

cat optionsfile | xargs gcc

Edit: I've been downvoted because Laurent doesn't know how xargs works, so here's the proof:

$ echo "-o output -Wall -Werro" > optionsfile
$ cat optionsfile | xargs -t gcc
gcc -o output -Wall -Werro
i686-apple-darwin10-gcc-4.2.1: no input files

The -t flag causes the command to be written to stderr before executing.

like image 25
Carl Norum Avatar answered Sep 17 '22 19:09

Carl Norum


gcc `cat file.with.options`
like image 32
ring bearer Avatar answered Sep 21 '22 19:09

ring bearer