bash:以空格作为参数传递路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7535677/
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
bash: passing paths with spaces as parameters?
提问by fbrereto
I have a bash script that recieves a set of files from the user. These files are sometimes under directories with spaces in their names. Unfortunately unlike this questionall the filenames are passed via the command line interface. Let's assume the paths are correctly quoted as they are passed in by the user, so spaces (save for quoted spaces) are delimiters between paths. How would I forward these parameters to a subroutine within my bash script in a way that preserves the quoted spaces?
我有一个 bash 脚本,它从用户那里接收一组文件。这些文件有时位于名称中带有空格的目录下。不幸的是,与这个问题不同,所有文件名都是通过命令行界面传递的。让我们假设路径在用户传入时被正确引用,因此空格(带引号的空格除外)是路径之间的分隔符。我将如何以保留引号空格的方式将这些参数转发到我的 bash 脚本中的子例程?
回答by Roland Illig
#! /bin/bash
for fname in "$@"; do
process-one-file-at-a-time "$fname"
done
Note the excessive use of quotes. It's all necessary.
请注意引号的过度使用。这都是必要的。
Passing all the arguments to another program is even simpler:
将所有参数传递给另一个程序甚至更简单:
process-all-together "$@"
The tricky case is when you want to split the arguments in half. That requires a lot more code in a simple POSIX shell. But maybe the Bash has some special features.
棘手的情况是当您想将参数一分为二时。这需要在简单的 POSIX shell 中编写更多代码。但也许 Bash 有一些特殊的功能。
回答by P.T.
You want "$@", which has the special syntax of expanding $@but preserving the white-space quoting of the caller (it does not create a single giant string with all the arguments in it). So someone can call your script like:
您想要"$@",它具有扩展$@但保留调用者的空白引用的特殊语法(它不会创建包含所有参数的单个巨大字符串)。所以有人可以像这样调用你的脚本:
bash-script.sh AFile "Another File With Spaces"
Then in your script you can do things like:
然后在您的脚本中,您可以执行以下操作:
for f in "$@"; do
echo "$f";
done
and get two lines of output (not 5).
并获得两行输出(不是 5 行)。
Read the paragraph about the Special Parameter "@" here: http://www.gnu.org/s/bash/manual/bash.html#Special-Parameters
在此处阅读有关特殊参数“@”的段落:http: //www.gnu.org/s/bash/manual/bash.html#Special-Parameters
回答by Sedat Kilinc
Bravo @Roland. Thans a lot for your solution
布拉沃@Roland。这对您的解决方案来说非常重要
It has really worked!
它真的奏效了!
I wrote a simple script function that opens a given path with nautilus.
我写了一个简单的脚本函数,用 nautilus 打开给定的路径。
And I've just nested a function with this "helper"-for-loop into the main function:
我刚刚将一个带有“helper”-for-loop 的函数嵌套到主函数中:
fmp () {
fmp2() {
nautilus "$@";
};
for fname in "$@";
do
fmp2 "$fname";
done;
}
Now I'm able to make all my scripts work handling with paths just by turning them into nested functions wrapped by a function with this helper-for-loop.
现在,我只需将所有脚本转换为由具有此辅助循环的函数包装的嵌套函数,就可以使我的所有脚本都能处理路径。
回答by ikegami
"$var"
For example,
例如,
$ var='foo bar'
$ perl -E'say "<<$_>>" for @ARGV' $var
<<foo>>
<<bar>>
$ perl -E'say "<<$_>>" for @ARGV' "$var"
<<foo bar>>

