How do I turn file content into an argument for a Unix command?
如何将文件内容转换为Unix命令的参数?
Turning an argument into file content is done as:
将参数转换为文件内容如下:
echo ABC > file.txt
But the other direction?
但是另一个方向呢?
7 个解决方案
#1
146
If your shell is bash (amongst others), a shortcut for $(cat afile)
is $(< afile)
, so you'd write:
如果您的shell是bash(其中之一),$(cat afile)的快捷方式是$(< afile),那么您应该这样写:
mycommand "$(< file.txt)"
Documented in the bash man page in the 'Command Substitution' section.
在“命令替换”一节中的bash手册页中记录。
Alterately, have your command read from stdin, so: mycommand < file.txt
另外,让您的命令从stdin中读取,so: mycommand < file.txt
#2
31
As already mentioned, you can use the backticks or $(cat filename)
.
如前所述,您可以使用backticks或$(cat文件名)。
What was not mentioned, and I think is important to note, is that you must remember that the shell will break apart the contents of that file according to whitespace, giving each "word" it finds to your command as an argument. And while you may be able to enclose a command-line argument in quotes so that it can contain whitespace, escape sequences, etc., reading from the file will not do the same thing. For example, if your file contains:
没有提到的是,我认为需要注意的是,您必须记住shell将根据空格分解文件的内容,并将它发现的每个“单词”作为参数提供给您的命令。虽然您可以将命令行参数括在引号中,以便它可以包含空格、转义序列等,但是从文件中读取不会做同样的事情。例如,如果您的文件包含:
a "b c" d
the arguments you will get are:
你会得到的论点是:
a
"b
c"
d
If you want to pull each line as an argument, use the while/read/do construct:
如果你想把每一行作为参数,使用while/read/do构造:
while read i ; do command_name $i ; done < filename
#3
13
You do that using backticks:
你可以用背勾:
echo World > file.txt
echo Hello `cat file.txt`
#4
9
command `< file`
will pass file contents to the command on stdin, but will strip newlines, meaning you couldn't iterate over each line individually. For that you could write a script with a 'for' loop:
将把文件内容传递给stdin上的命令,但将删除换行符,这意味着不能逐个迭代每一行。为此,您可以编写一个带有“For”循环的脚本:
for i in `cat input_file`; do some_command $i; done
#5
5
If you want to do this in a robust way that works for every possible command line argument (values with spaces, values with newlines, values with literal quote characters, non-printable values, values with glob characters, etc), it gets a bit more interesting.
如果您想要以一种健壮的方式来处理每一个可能的命令行参数(带空格的值、带换行的值、带文字引号字符的值、不可打印的值、带通配符的值等等),那么它会变得更有趣一些。
To write to a file, given an array of arguments:
要写入文件,给定一个参数数组:
printf '%s\0' "${arguments[@]}" >file
...replace with "argument one"
, "argument two"
, etc. as appropriate.
…用“第一论点”、“第二论点”等词替换。
To read from that file and use its contents (in bash, ksh93, or another recent shell with arrays):
读取该文件并使用其内容(在bash、ksh93或另一个具有数组的最近shell中):
declare -a args=()
while IFS='' read -r -d '' item; do
args+=( "$item" )
done <file
run_your_command "${args[@]}"
To read from that file and use its contents (in a shell without arrays; note that this will overwrite your local command-line argument list, and is thus best done inside of a function, such that you're overwriting the function's arguments and not the global list):
读取该文件并使用其内容(在没有数组的shell中;注意,这将覆盖您的本地命令行参数列表,因此最好在函数内部完成,这样您就覆盖了函数的参数,而不是全局列表):
set --
while IFS='' read -r -d '' item; do
set -- "$@" "$item"
done <file
run_your_command "$@"
Note that -d
(allowing a different end-of-line delimiter to be used) is a non-POSIX extension, and a shell without arrays may also not support it. Should that be the case, you may need to use a non-shell language to transform the NUL-delimited content into an eval
-safe form:
注意-d(允许使用不同的行尾分隔符)是一个非posix扩展,没有数组的shell也不支持它。如果是这种情况,您可能需要使用非shell语言将空分隔的内容转换为事件安全形式:
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 "$@"
#6
4
Here's how I pass contents of a file as an argument to a command:
下面介绍如何将文件的内容作为参数传递给命令:
./foo --bar "$(cat ./bar.txt)"
#7
3
In my bash shell the following worked like a charm:
在我的bash shell中,以下操作就像一个咒语:
cat input_file | xargs -I % sh -c 'command1 %; command2 %; command3 %;'
where input_file is
input_file在哪里
arg1
arg2
arg3
As evident, this allows you to execute multiple commands with each line from input_file, a nice little trick I learned here.
显然,这允许您对input_file中的每一行执行多个命令,这是我在这里学到的一个小技巧。
#1
146
If your shell is bash (amongst others), a shortcut for $(cat afile)
is $(< afile)
, so you'd write:
如果您的shell是bash(其中之一),$(cat afile)的快捷方式是$(< afile),那么您应该这样写:
mycommand "$(< file.txt)"
Documented in the bash man page in the 'Command Substitution' section.
在“命令替换”一节中的bash手册页中记录。
Alterately, have your command read from stdin, so: mycommand < file.txt
另外,让您的命令从stdin中读取,so: mycommand < file.txt
#2
31
As already mentioned, you can use the backticks or $(cat filename)
.
如前所述,您可以使用backticks或$(cat文件名)。
What was not mentioned, and I think is important to note, is that you must remember that the shell will break apart the contents of that file according to whitespace, giving each "word" it finds to your command as an argument. And while you may be able to enclose a command-line argument in quotes so that it can contain whitespace, escape sequences, etc., reading from the file will not do the same thing. For example, if your file contains:
没有提到的是,我认为需要注意的是,您必须记住shell将根据空格分解文件的内容,并将它发现的每个“单词”作为参数提供给您的命令。虽然您可以将命令行参数括在引号中,以便它可以包含空格、转义序列等,但是从文件中读取不会做同样的事情。例如,如果您的文件包含:
a "b c" d
the arguments you will get are:
你会得到的论点是:
a
"b
c"
d
If you want to pull each line as an argument, use the while/read/do construct:
如果你想把每一行作为参数,使用while/read/do构造:
while read i ; do command_name $i ; done < filename
#3
13
You do that using backticks:
你可以用背勾:
echo World > file.txt
echo Hello `cat file.txt`
#4
9
command `< file`
will pass file contents to the command on stdin, but will strip newlines, meaning you couldn't iterate over each line individually. For that you could write a script with a 'for' loop:
将把文件内容传递给stdin上的命令,但将删除换行符,这意味着不能逐个迭代每一行。为此,您可以编写一个带有“For”循环的脚本:
for i in `cat input_file`; do some_command $i; done
#5
5
If you want to do this in a robust way that works for every possible command line argument (values with spaces, values with newlines, values with literal quote characters, non-printable values, values with glob characters, etc), it gets a bit more interesting.
如果您想要以一种健壮的方式来处理每一个可能的命令行参数(带空格的值、带换行的值、带文字引号字符的值、不可打印的值、带通配符的值等等),那么它会变得更有趣一些。
To write to a file, given an array of arguments:
要写入文件,给定一个参数数组:
printf '%s\0' "${arguments[@]}" >file
...replace with "argument one"
, "argument two"
, etc. as appropriate.
…用“第一论点”、“第二论点”等词替换。
To read from that file and use its contents (in bash, ksh93, or another recent shell with arrays):
读取该文件并使用其内容(在bash、ksh93或另一个具有数组的最近shell中):
declare -a args=()
while IFS='' read -r -d '' item; do
args+=( "$item" )
done <file
run_your_command "${args[@]}"
To read from that file and use its contents (in a shell without arrays; note that this will overwrite your local command-line argument list, and is thus best done inside of a function, such that you're overwriting the function's arguments and not the global list):
读取该文件并使用其内容(在没有数组的shell中;注意,这将覆盖您的本地命令行参数列表,因此最好在函数内部完成,这样您就覆盖了函数的参数,而不是全局列表):
set --
while IFS='' read -r -d '' item; do
set -- "$@" "$item"
done <file
run_your_command "$@"
Note that -d
(allowing a different end-of-line delimiter to be used) is a non-POSIX extension, and a shell without arrays may also not support it. Should that be the case, you may need to use a non-shell language to transform the NUL-delimited content into an eval
-safe form:
注意-d(允许使用不同的行尾分隔符)是一个非posix扩展,没有数组的shell也不支持它。如果是这种情况,您可能需要使用非shell语言将空分隔的内容转换为事件安全形式:
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 "$@"
#6
4
Here's how I pass contents of a file as an argument to a command:
下面介绍如何将文件的内容作为参数传递给命令:
./foo --bar "$(cat ./bar.txt)"
#7
3
In my bash shell the following worked like a charm:
在我的bash shell中,以下操作就像一个咒语:
cat input_file | xargs -I % sh -c 'command1 %; command2 %; command3 %;'
where input_file is
input_file在哪里
arg1
arg2
arg3
As evident, this allows you to execute multiple commands with each line from input_file, a nice little trick I learned here.
显然,这允许您对input_file中的每一行执行多个命令,这是我在这里学到的一个小技巧。