比如说,我有一个文件foo.txt,指定了N个参数

arg1
arg2
...
argN

我需要传递给命令my_command

如何使用文件的行作为命令的参数?


你可以使用反勾:

echo World > file.txt
echo Hello `cat file.txt`

如果你的shell是bash, $(cat afile)的快捷方式是$(< afile),所以你可以这样写:

mycommand "$(< file.txt)"

bash手册页中的“命令替换”部分中有详细说明。

或者,让你的命令从stdin读取,这样:mycommand < file.txt


command `< file`

将文件内容传递给stdin上的命令,但将删除换行符,这意味着您不能单独遍历每一行。为此,你可以写一个带有' For '循环的脚本:

for line in `cat input_file`; do some_command "$line"; done

或者(多行变体):

for line in `cat input_file`
do
    some_command "$line"
done

或者(多行变体,用$()代替' '):

for line in $(cat input_file)
do
    some_command "$line"
done

引用:

对于循环语法:https://www.cyberciti.biz/faq/bash-for-loop/


如前所述,可以使用反撇号或$(cat文件名)。

我认为有必要注意的一点是,您必须记住shell将根据空白分隔该文件的内容,并将它找到的每个“单词”作为参数提供给您的命令。虽然您可以将命令行参数括在引号中,以便它可以包含空格、转义序列等,但从文件中读取不会做同样的事情。例如,如果你的文件包含:

a "b c" d

你会得到的论点是:

a
"b
c"
d

如果你想拉出每一行作为参数,使用while/read/do结构:

while read i ; do command_name $i ; done < filename

在我的bash shell下面的工作就像一个魅力:

cat input_file | xargs -I % sh -c 'command1 %; command2 %; command3 %;'

input_file在哪里

arg1
arg2
arg3

显然,这允许您对input_file中的每一行执行多个命令,这是我在这里学到的一个不错的小技巧。


如果您希望以一种适用于所有可能的命令行参数(带有空格的值、带有换行符的值、带有文字引号的值、不可打印的值、带有glob字符的值等)的健壮方式来执行此操作,则会变得更加有趣。


写入文件,给定一个参数数组:

printf '%s\0' "${arguments[@]}" >file

…替换为“论点一”、“论点二”等。


要从该文件读取并使用其内容(在bash、ksh93或其他最新的带有数组的shell中):

declare -a args=()
while IFS='' read -r -d '' item; do
  args+=( "$item" )
done <file
run_your_command "${args[@]}"

要从该文件读取并使用其内容(在没有数组的shell中;注意,这将覆盖你的本地命令行参数列表,因此最好在函数内部完成,这样你就覆盖了函数的参数而不是全局列表):

set --
while IFS='' read -r -d '' item; do
  set -- "$@" "$item"
done <file
run_your_command "$@"

注意-d(允许使用不同的行结束分隔符)是非posix扩展,没有数组的shell也可能不支持它。如果是这样的话,你可能需要使用非shell语言将null分隔的内容转换为eval安全的形式:

quoted_list() {
  ## Works with either Python 2.x or 3.x
  python -c '
import sys, pipes, shlex
quote = pipes.quote if hasattr(pipes, "quote") else shlex.quote
print(" ".join([quote(s) for s in sys.stdin.read().split("\0")][:-1]))
  '
}

eval "set -- $(quoted_list <file)"
run_your_command "$@"

下面是我如何将文件内容作为参数传递给命令:

./foo --bar "$(cat ./bar.txt)"

如果你所需要做的就是将文件arguments.txt的内容

arg1
arg2
argN

进入my_command arg1 arg2 argN,然后你可以简单地使用xargs:

xargs -a arguments.txt my_command

你可以在xargs调用中添加额外的静态参数,比如xargs -a arguments.txt my_command staticArg,它将调用my_command staticArg arg1 arg2 argN


在编辑了几次@Wesley Rice的答案后,我觉得我的改变太大了,不能继续修改他的答案,而不是写我自己的答案。所以,我决定自己写一个!

读取文件中的每一行,并像这样逐行操作它:

#!/bin/bash
input="/path/to/txt/file"
while IFS= read -r line
do
  echo "$line"
done < "$input"

这直接来自作者Vivek Gite: https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/。功劳归他!

语法:在Bash Unix和Linux shell上逐行读取文件: 1. bash、ksh、zsh和所有其他shell逐行读取文件的语法如下 2. While read -r line;命令;Done < input.file 3.传递给read命令的-r选项可防止反斜杠转义被解释。 4. 在read命令前添加IFS= option,以防止前导/尾随空格被修剪 5. if = read -r line;do COMMAND_on $lineDone < input.file


现在来回答这个现在已经结束的问题,我也有:它是否可能' git添加'一个文件的文件列表?-这是我的答案:

Note that FILES_STAGED is a variable containing the absolute path to a file which contains a bunch of lines where each line is a relative path to a file I'd like to do git add on. This code snippet is about to become part of the "eRCaGuy_dotfiles/useful_scripts/sync_git_repo_to_build_machine.sh" file in this project, to enable easy syncing of files in development from one PC (ex: a computer I code on) to another (ex: a more powerful computer I build on): https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.

while IFS= read -r line
do
    echo "  git add \"$line\""
    git add "$line" 
done < "$FILES_STAGED"

引用:

我从哪里复制了我的答案:https://www.cyberciti.biz/faq/unix-howto-read-line-by-line-from-file/ 对于循环语法:https://www.cyberciti.biz/faq/bash-for-loop/

相关:

如何逐行读取文件的内容,并做git添加它:它是可能的' git添加'从一个文件的文件列表?


我建议使用:

command $(echo $(tr '\n' ' ' < parameters.cfg))

只需修改结束行字符并将其替换为空格,然后使用echo将结果字符串作为可能的单独参数推入。


这些答案似乎都不适合我,也不是太复杂。幸运的是,xargs并不复杂(在Ubuntu 20.04上测试)。

正如OP提到的那样,这适用于文件中单独一行的每个参数,也是我所需要的。

cat foo.txt | xargs my_command

需要注意的一点是,它似乎不适用于别名命令。

如果命令接受一个字符串中包装的多个参数,则接受的答案有效。在我使用(Neo)Vim的情况下,它不会和参数都粘在一起。

Xargs正确地做到了这一点,实际上为命令提供了单独的参数。


这两种解决方案即使在行中有空格时也有效:

readarray -t my_args < foo.txt
my_command "${my_args[@]}"

如果readarray不起作用,用mapfile代替它,它们是同义词。

我以前尝试过下面这个,但当my_command是一个脚本时有问题:

xargs -d '\n' -a foo.txt my_command