How to Use the Lines of a File as Arguments of a Command

How do I use the lines of a file as arguments of a command?

If your shell is bash (amongst others), a shortcut for $(cat afile) is $(< afile), so you'd write:

mycommand "$(< file.txt)"

Documented in the bash man page in the 'Command Substitution' section.

Alterately, have your command read from stdin, so: mycommand < file.txt

Command line arguments, reading a file

You can use int main(int argc, char **argv) as your main function.

argc - will be the count of input arguments to your program.

argv - will be a pointer to all the input arguments.

So, if you entered C:\myprogram myfile.txt to run your program:

  • argc will be 2
  • argv[0] will be myprogram.
  • argv[1] will be myfile.txt.

More details can be found here

To read the file:

FILE *f = fopen(argv[1], "r"); // "r" for read

For opening the file in other modes, read this.

Pass content from .txt file as argument for bash script?

$(command) returns the output of the command. If you do $(cat some_file) it will return the text of the file. You can use it to give the content of a file as an argument doing:

cmd1 $(cat args_file)

So when you use echo $(cat file.txt), you get the same output as if you were using cat file.txt because cat sends the content of the file to echo which displays it to the standard output.

$n means argument n passed to the script ($0 being the name of the script). Here you simply have to provide one argument, the name of the file. So $2, $3 and $4 will not contain anything.

So, from the file you can only get a string with the names with $names=$(cat $1). In order to get each field separately, you can use cut:

lname=$(cut -d \  -f 1 $1)
fname=$(cut -d \ -f 2 $1)
mname=$(cut -d \ -f 3 $1)
group=$(cut -d \ -f 4 $1)

NOTES:

The symbol for comments in shell is # NOT //.

head displays the first lines of a file, head -c the first bytes. It does not cut the file.

How to use file contents as command-line arguments?

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.

How to read command line arguments from a text file?

If the reading of the parameters must be done solely from the file having it's name, the idiomatic way is, I would say, to use getline().

std::ifstream ifs("text.txt");
if (!ifs)
std::cerr << "couldn't open text.txt for reading\n";
std::string line;
std::getline(ifs, line);
int integer = std::stoi(line);
std::getline(ifs, line);
std::string string1 = line;
std::getline(ifs, line);
std::string string2 = line;

Because there are little lines in your file, we can allow ourselves some repetition. But as it becomes larger, you might need to read them into a vector:

std::vector<std::string> arguments;
std::string line;
while (getline(ifs, line))
arguments.push_back(line);

There some optimizations possible, as reusing the line buffer in the first example and using std::move(), but they are omitted for clarity.



Related Topics



Leave a reply



Submit