在 bash 中列出目录路径中带有空格的文件时出现问题
当在命令行中输入目录时,将显示以下内容:
ls -d -1 "/Volumes/Development/My Project/Project"/**/* | grep \.png$
打印所有以 .png
结尾的文件的列表。
但是,当我尝试创建脚本时:
#! /bin/bash
clear ;
# Tempoary dir for processing
mkdir /tmp/ScriptOutput/ ;
wdir="/Volumes/Development/My Project/Project" ;
echo "Working Dir: $wdir" ;
# Get every .PNG file in the project
for image in `ls -d -1 "$wdir"/**/* | grep \.png$`; do
...
done
我收到错误:
cp: /Volumes/Development/My: No such file or directory
space 导致问题,但我不知道为什么?
When entered directory into the command line, this:
ls -d -1 "/Volumes/Development/My Project/Project"/**/* | grep \.png$
Prints a list of all the file ending in .png
.
However when I try and create a script:
#! /bin/bash
clear ;
# Tempoary dir for processing
mkdir /tmp/ScriptOutput/ ;
wdir="/Volumes/Development/My Project/Project" ;
echo "Working Dir: $wdir" ;
# Get every .PNG file in the project
for image in `ls -d -1 "$wdir"/**/* | grep \.pngWhen entered directory into the command line, this:
ls -d -1 "/Volumes/Development/My Project/Project"/**/* | grep \.png$
Prints a list of all the file ending in .png
.
However when I try and create a script:
; do
...
done
I get the error:
cp: /Volumes/Development/My: No such file or directory
The space is causing an issue, but I don't know why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种选择是更改 IFS:
在
man bash
中了解有关 IFS 的更多信息。Another option is to change IFS:
Read more about IFS in
man bash
.使用更多引号和不解析
ls
输出。Use more quotes and don't parse
ls
output.如果您喜欢使用 while read 和由管道创建的子进程,您可以:
If you fine with using
while read
and subprocess created by pipe, you can:您可以尝试用 [[:space:]] 代替空格
或执行命令来转换单个空格
you can try, [[:space:]] in place of space
or execute command to convert single space