如何从文件传递命令行参数 [英] How to pass command line parameters from a file
问题描述
我有一个从 argv 读取命令行参数的 C 程序.是否可以制作管道将文件内容作为命令行参数重定向到我的程序?假设我有一个包含以下内容的文件 arguments.dat
:
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
我尝试了以下方法:
cat arguments.dat | ./myprogram
没有成功.
推荐答案
对于大多数 shell,您可以使用 $(<filename)
将文件内容插入命令行:>
With most shells, you can insert the contents of a file into a command line with $(<filename)
:
./myprogram $(<arguments.dat)
如果您的 shell 不支持,那么旧的方法之一将起作用:
If your shell doesn't support that, then one of the older ways will work:
./myprogram $(cat arguments.dat)
./myprogram `cat arguments.dat` # need this one with csh/tcsh
(您确实知道命令行参数和文件输入之间的区别,对吗?您为什么希望将命令行参数通过管道传输到程序中?)
(You do know the difference between command line arguments and file input, right? Why would you expect to pipe command line arguments into a program?)
这篇关于如何从文件传递命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!