在Linux中,如何提取文件内容并作为命令行参数传递?

在Linux中,如何提取文件内容并作为命令行参数传递?

我需要运行一个命令:

python test.py command --option 1 value1 value2 value3 value4 value5

(最高价值 100)

我有一个包含空格分隔值的文本文件,例如:

值1 值2 值3 值4 值5..

我怎样才能将其放入命令中,而无需将整个文件内容复制并粘贴到终端中?

答案1

您可以使用 Bash 命令替换功能:

python test.py command --option 1 $(<file.txt)

man bash

Command substitution allows the output of a command to replace
the command name. There are two forms:

$(command)

or

`command`

Bash performs the expansion by executing command and replacing
the command substitution with the standard output of the command,
with any trailing newlines deleted. Embedded newlines are not
deleted, but they may be removed during word splitting. The
command substitution $(cat file) can be replaced by the
equivalent but faster $(< file).

答案2

你可以做:

python test.py command --option 1 `cat file.txt`

答案3

这是另一种方法:

xargs -a file.txt python test.py command --option 1

相关内容