使用 linux 如何将文件的内容作为参数传递给可执行文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4241369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:02:52  来源:igfitidea点击:

using linux how can I pass the contents of a file as a parameter to an executable?

linux

提问by al b.

let's say I want to run ./programwith a string argument

假设我想./program使用字符串参数运行

instead of typing ./program stringeach time, how can I do ./program <file>where <file>is a file that contains string?

而不是键入 ./program string每一次,我该怎么办./program <file>,其中<file>是包含的字符串的文件?

thankyou

谢谢你

采纳答案by robbrit

This should do the trick:

这应该可以解决问题:

./program `cat file`

If you want the entire file content in a single argument, add double quotation (tested in bash. I think It may vary shell to shell) :

如果您希望在单个参数中包含整个文件内容,请添加双引号(在 bash 中测试。我认为它可能因外壳而异):

./program "`cat file`" 

回答by John Kugelman

Command

命令

./program "$(< file)"

Explanation

解释

  1. $(cmd)runs a command and converts its output into a string. $(cmd)can also be written with backticks as `cmd`. I prefer the newer $(cmd)as it nests better than the old school backticks.

  2. The command we want is cat file. And then from the bash man page:

    The command substitution $(cat file)can be replaced by the equivalent but faster $(< file).

  3. The quotes around it make sure that whitespace is preserved, just in case you have spaces or newlines inside of your file. Properly quoting things in shell scripts is a skill that is sorely lacking. Quoting variables and command substitutions is a good habit to good into so you don't get bit later when your file names or variable values have spaces or control characters or other weirdnesses.

  1. $(cmd)运行命令并将其输出转换为字符串。$(cmd)也可以用反引号写成`cmd`. 我更喜欢新的,$(cmd)因为它比旧学校的反引号更好地嵌套。

  2. 我们想要的命令是cat file. 然后从 bash 手册页:

    命令替换$(cat file)可以替换为等效但更快的$(< file)

  3. 它周围的引号确保保留空格,以防万一您的文件中有空格或换行符。在 shell 脚本中正确引用内容是一项非常缺乏的技能。引用变量和命令替换是一个很好的习惯,所以当你的文件名或变量值有空格或控制字符或其他奇怪的东西时,你不会迟到。

回答by paxdiablo

You can use one of:

您可以使用以下之一:

./program $(cat file)
./program "$(cat file)"

The latter is useful if there may be multiple words in the file and you want them to be treated as a single argument. In any case, I prefer the use if $()to backticks simply due to it's ability to nest (which is probably not a requirementin this case).

如果文件中可能有多个单词并且您希望将它们视为单个参数,则后者很有用。在任何情况下,我更喜欢使用,如果$()以反引号只是由于它的能力巢(这可能不是一个要求,在这种情况下)。

Also keep in mind that this answer (and others) are more related to the shell in use rather than Linux itself. Since the predominant and the best :-)shell seems to be bash, I've coded specifically for that.

还要记住,这个答案(和其他答案)与使用的 shell 更相关,而不是 Linux 本身。由于主要和最好的 :-)shell 似乎是bash,我专门为此编写了代码。

This is actually a fairly common way of killing processes under UNIX lookalikes. A daemon (like cron) will write its process ID to a file (like /var/cron.pid) and you can signal it with something like:

这实际上是在 UNIX 相似程序下杀死进程的一种相当常见的方法。守护进程(如cron)会将其进程 ID 写入文件(如/var/cron.pid),您可以使用以下内容向其发送信号:

kill -HUP $(cat /var/cron.pid)

回答by Matthew

Another option is xargs:

另一种选择是 xargs:

cat file | xargs ./program

I personally find this version easier to read, especially if you're doing more than just catting a file.

我个人觉得这个版本更容易阅读,尤其是当你做的不仅仅是文件分类时。

From the man page:

从手册页:

xargs reads items from the standard input, delimited by blanks or newlines, and executes the command one or more times with any initial-arguments followed by items read from standard input.

xargs 从标准输入读取项目,以空格或换行符分隔,并使用任何初始参数执行命令一次或多次,后跟从标准输入读取的项目。

In plain speak, xargs will execute ./program on each line/word that is being piped to it.

简单地说, xargs 将在通过管道传输到它的每一行/单词上执行 ./program 。

See man xargs for more options.

有关更多选项,请参阅 man xargs。

回答by Jon Deaton

All the above answers work but I find the cleanest and easiest to type option is

以上所有答案都有效,但我发现最干净、最容易输入的选项是

xargs < file ./program

回答by Habeeb Perwad

This is extension to @robbrit answer.

这是@robbrit 答案的扩展。

Consider a file.txtwith content:

考虑一个包含内容的file.txt

1 2 3
4 5 6

And the a program.cwith content:

还有一个包含内容的program.c

#include<stdio.h>
void main(int argc, char** argv) 
{ 
    for (int i = 0; i < argc; i++) 
        printf("Arg %d: %s\n", i, argv[i]); 
}

Here is what happen if you pass command line arguments with quotes and without:

如果您传递带引号和不带引号的命令行参数,会发生以下情况:

$ gcc  program.c -o program

$ ./program `cat file`
Arg 0: ./program
Arg 1: 1
Arg 2: 2
Arg 3: 3
Arg 4: 4
Arg 5: 5
Arg 6: 6

$ ./program "`cat file`"
Arg 0: ./program
Arg 1: 1 2 3
4 5 6