sh - 如何遍历 FreeBSD/bin/sh 中包含空格的文件

标签 sh freebsd

我正在尝试使用 FreeBSD/bin/sh shell 驱动一组包含空间的文件:

user@host:~/temp/2011 $ find . | grep '03-11'
./01jan/ES 03-11.txt
./02feb/ES 03-11.txt
./03mar/ES 03-11.txt
./04apr/ES 03-11.txt
./05may/ES 03-11.txt
user@host:~/temp/2011 $ for i in `find . | grep '03-11'`; do echo "$i"; done
./01jan/ES
03-11.txt
./02feb/ES
03-11.txt
./03mar/ES
03-11.txt
./04apr/ES
03-11.txt
./05may/ES
03-11.txt

虽然我想得到:

user@host:~/temp/2011 $ find . | grep '03-11'
./01jan/ES 03-11.txt
./02feb/ES 03-11.txt
./03mar/ES 03-11.txt
./04apr/ES 03-11.txt
./05may/ES 03-11.txt
user@host:~/temp/2011 $ for i in `find . | grep '03-11'`; do echo "$i"; done
./01jan/ES 03-11.txt
./02feb/ES 03-11.txt
./03mar/ES 03-11.txt
./04apr/ES 03-11.txt
./05may/ES 03-11.txt

在这种情况下应该如何处理空间?

最佳答案

您需要将 IFS 设置为不包含空格字符的值。

例如:

(IFS=$'\n'; for i in `find . | grep '03-11'`; do echo "$i"; done)

参见 manual page了解详情。

关于sh - 如何遍历 FreeBSD/bin/sh 中包含空格的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65275503/

相关文章:

java - 在 Linux 上安装 Java EE SDK 时出错

linux - 我怎样才能从 shell 脚本获得各种输出?

linux - 我想读取第一行值并从第二行值中减去它并继续直到结束

apache - Mercurial 不会在系统启动时启动 [FreeBSD 9.0 Apache22]

ssh - 在远程服务器上发出命令时如何允许输入密码?

macos - 强行触发kevent

ruby-on-rails - FreeBSD 操作系统中的 Ruby On Rails 安装 -libv8 中出现错误

linux - 使用exec命令时无法退出Shell脚本

Linux:从文件中读取字符串并在其他脚本中执行命令

version-control - darcs:第一次推送失败