如何从文件传递命令行参数 [英] How to pass command line parameters from a file
问题描述
我有一个C程序,可从argv读取命令行参数.是否可以通过管道将文件内容作为命令行参数重定向到我的程序?假设我有一个文件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)
如果您的外壳不支持该功能,则可以使用一种较旧的方法:
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屋!