路径名中的空格会导致在 Bash 中查找时出现问题。有什么*简单的*解决方法吗?
有什么方法可以更改以下字符串,这样当文件/文件夹中包含空格时我就不会遇到任何问题?
files=`find ~/$folder -name "*@*" -type f`
我希望有一个解决方案,不必涉及更改代码的其他部分,但需要更改这行代码,因为除了这个小细节之外,一切似乎都工作正常。
谢谢
编辑:这是更详细的代码:
abc=( $(find "$pasta" -name "$ficheiro_original@*" -type f) )
abc_length=${#abc[@]}
Is there any way to change the following string so I don't get any problems when there are files/folders with spaces in them?
files=`find ~/$folder -name "*@*" -type f`
I'd prefer if there was a solution that wouldn't have to involve having to change other parts of my code but this line of code, as everything seems to be working correctly, apart from this minor detail.
Thanks
EDIT: Here is the code in a bit more detail:
abc=( $(find "$pasta" -name "$ficheiro_original@*" -type f) )
abc_length=${#abc[@]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您稍后在脚本中不使用这些文件名,只需迭代它们并即时处理即可。
否则,您可以设置 IFS
\n' files=$(find ~/$folder -name "*@*" -type f)更新:
If you are not using those file names later in your script , just iterate them and process on the fly.
otherwise, you can set IFS
\n' files=$(find ~/$folder -name "*@*" -type f)Update:
您必须进行一些更改,但要处理任意名称,请考虑使用 GNU Find 选项
-print0
等。(其中表示为“^@
”的单字节实际上是 ASCII NUL(“\0”;使用 Control-V 输入 Control-Shift-@ )。分隔符的空字符串表示“使用零字节 ASCII NUL 作为分隔符”,适合解析“
find ... -print0
”输出(谢谢。 Dennis Williamson 获取提示。)这允许您读取任意名称,您可能应该使用 bash 数组来读取。保留名称,但这意味着脚本中需要进行一些更改
(考虑到只需要担心空格的注释响应,这可能有点过头了,尽管使用
read
来处理具有名称的行是可行的。操作的关键部分,使用数组可能会让生活变得更简单。)You'd have to make some changes, but to deal with arbitrary names, think in terms of using the GNU Find option
-print0
, etc.(Where the single byte represented as '^@
' is actually an ASCII NUL ('\0'; enter with Control-V Control-Shift-@).The empty string for the delimiter means 'use the zero byte, ASCII NUL, as the delimiter' and is appropriate for parsing '
find ... -print0
' output. (Thanks Dennis Williamson for the tip.)This allows you to read any arbitrary names. You should probably use a bash array to hold the names, but that implies some changes further down the script.
(Given the comment response that only spaces have to be worried about, this might be overkill, though using
read
to process lines with the names is a key part of the operation, and using an array would probably make life simpler.)如果您需要名称中可能包含空格的文件列表,则几乎必须将它们存储为数组,而不仅仅是字符串。使用如下内容创建数组:
然后您必须修改脚本的其余部分以将文件用作数组而不是字符串,并且它(和其他文件名)应始终用双引号引起来以保留空格被误认为是分隔符。例如,在您当前使用
$files
的任何地方,请将其替换为"${files[@]}"
If you need a list of files that might have spaces in the names, you pretty much have to store them as an array, rather than just a string. Create the array with something like this:
and then you'll have to modify the rest of the script to use files as an array rather than a string, and it (and other filenames) should always be in double-quotes to keep spaces from being mistaken as separators. For instance, anyplace you're currently using
$files
, replace that with"${files[@]}"
这是另一种无需更改其余代码即可解决的方法:
它很脏,不适用于具有特殊字符的文件名,例如
"
。它使用eval
来评估字符串Bash 数组的-printf
的find
来形成该字符串,我个人更喜欢更改
$IFS
,仅供参考。Here is another way to get around without changing the rest of code:
It's dirty and will not work for filename with special characters, e.g.
"
. It useseval
to evaluate a string of a Bash array and-printf
offind
to form that string.I personally prefer changing
$IFS
, just FYI.要将带有空格的文件名读取到 Bash 数组变量中,您也可以使用“read”内置命令:
请注意,新设置的 IFS 变量的范围仅限于执行 read 命令(这会保留原始 IFS 变量)完好无损的)。
To read file names with spaces into a Bash array variable, you could use the "read" builtin command as well:
Note that the scope of the newly set IFS variable is limited to the execution of the read command (which leaves the original IFS variable intact).