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 2argv[0]
will bemyprogram
.argv[1]
will bemyfile.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
How to Join Multiple Lines of File Names into One With Custom Delimiter
How to Run Script as Another User Without Password
Command Line Program to Create Website Screenshots (On Linux)
How to Use Sed to Change My Configuration Files, With Flexible Keys and Values
Cronjob Does Not Execute a Script That Works Fine Standalone
How to Print the Ld(Linker) Search Path
Use Sudo With Password as Parameter
Linking Openssl Libraries to a Program
Why Do X86-64 Linux System Calls Modify Rcx, and What Does the Value Mean
Tool to Trace Local Function Calls in Linux
Shell Script: Run Function from Script Over Ssh
Syntax Error in Shell Script With Process Substitution
Lost Httpd.Conf File Located Apache
How to Quickly Sum All Numbers in a File
How to Get Docker Linux Container Information from Within the Container Itself
How to Generate New Variable Names on the Fly in a Shell Script
Why Do You Need to Put #!/Bin/Bash At the Beginning of a Script File
Assembling 32-Bit Binaries on a 64-Bit System (Gnu Toolchain)