bash 如何使用文件内容作为命令行参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2511496/
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-09-17 21:51:46  来源:igfitidea点击:

How to use file contents as command-line arguments?

linuxbash

提问by morfis

I'd like to use arguments from file as command-line arguments for some commands like gcc or ls.

我想将文件中的参数用作某些命令(如 gcc 或 ls)的命令行参数。

For example gcc -o output -Wall -Werro

例如 gcc -o output -Wall -Werro

as file consist of:

作为文件组成:

-o output -Wall -Werro

-o 输出 -Wall -Werro

Used for gcc command-line call.

用于 gcc 命令行调用。

回答by cartland

Some programs use the "@" semantics to feed in args from a file eg. gcc @argfile

一些程序使用“@”语义从文件中输入参数,例如。海湾合作委员会@argfile

Where, for gcc, argfile contains options

其中,对于 gcc,argfile 包含选项

-ansi
-I/usr/include/mylib

This can be nested so that argfile can contain

这可以嵌套,以便 argfile 可以包含

-ansi
-I/usr/include/mylib
@argfile2

http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Overall-Options.html#Overall-Options

http://gcc.gnu.org/onlinedocs/gcc-4.6.3/gcc/Overall-Options.html#Overall-Options

回答by Carl Norum

You can use xargs:

您可以使用xargs

cat optionsfile | xargs gcc

Edit: I've been downvoted because Laurent doesn't know how xargsworks, so here's the proof:

编辑:我被否决了,因为 Laurent 不知道如何xargs工作,所以这里是证据:

$ 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 -tflag causes the command to be written to stderrbefore executing.

-t标志导致stderr在执行之前写入命令。

回答by ring bearer

gcc `cat file.with.options`

回答by R Samuel Klatchko

I recommend using $()along with cat:

我建议$()与 cat 一起使用:

gcc $(cat file)

The nice thing about $()over backticks (the classic way) is that it is easier to nest one within another:

关于$()反引号(经典方式)的好处是更容易将一个嵌套在另一个中:

gcc $(cat $(cat filename))

回答by rz0

Most of the time, command substitution (either through backticks or $(), as others have pointed out) is fine, but beware of shell expansion rules. Especially, keep in mind that unquoting is done beforeand word splitting is done aftercommand substitution.

大多数情况下,命令替换(通过反引号或 $(),正如其他人指出的那样)很好,但要注意 shell 扩展规则。尤其要记住,命令替换之前进行取消引用 ,命令替换之后进行分词。

This is not too bad if all your arguments are words but if you start putting spaces or other special characters that would normally need to be quoted into your arguments, then you may meet with strange results (notice the abnormal spacing and quotes in the output):

如果您的所有参数都是单词,这还不错,但是如果您开始在参数中添加空格或其他通常需要引用的特殊字符,那么您可能会遇到奇怪的结果(注意输出中的异常间距和引号) :

$ echo "foo 'bar   baz'" >a
$ echo $(cat a)
foo 'bar baz'

Quoting the whole command subtitution is not a solution, obviously, as it would prevent word splitting (hence, your whole file's content would appear as one long argument instead of many options), and would do nothing about the quotes.

引用整个命令替换显然不是解决方案,因为它会防止分词(因此,整个文件的内容将显示为一个长参数而不是许多选项),并且不会对引号执行任何操作。

$ echo "$(cat a)"
foo 'bar   baz'

One solution around this is to use the eval builtin:

解决此问题的一种解决方案是使用 eval 内置命令:

$ eval echo "$(cat a)"
foo bar   baz

N.B.: echo may not be the best example command here; you might want to replace it with something else, e.g. the following function:

注意:echo 可能不是这里最好的示例命令;您可能想用其他东西替换它,例如以下功能:

$ f() { echo $#; }

回答by ghostdog74

with bash

用 bash

gcc $(<file)