bash 问题在bash中列出目录路径中的空格的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6897190/
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
Problem Listing files in bash with spaces in directory path
提问by Richard Stelling
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.
打印所有以.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 \.png$`; do
...
done
I get the error:
我收到错误:
cp: /Volumes/Development/My: No such file or directory
The spaceis causing an issue, but I don't know why?
将space导致一个问题,但我不知道为什么?
回答by Micha? ?rajer
Another option is to change IFS:
另一种选择是更改 IFS:
OLDIFS="$IFS" # save it
IFS="" # don't split on any white space
for file in `ls -R . | grep png`
do
echo "$file"
done
IFS=$OLDIFS # restore IFS
Read more about IFS in man bash.
在 中阅读有关 IFS 的更多信息man bash。
回答by l0b0
回答by Yajushi
you can try, [[:space:]] in place of space
你可以试试用 [[:space:]] 代替空格
wdir="/Volumes/Development/My[[:space:]]Project/Project"
or execute command to convert single space
或执行命令转换单个空格
wdir=`echo "$wdir" | sed 's/[[:space:]]/\[[:space:]]/g'`
回答by Micha? ?rajer
If you fine with using while readand subprocess created by pipe, you can:
如果您可以使用while read管道创建的子进程,则可以:
find . -name '*.png' | while read FILE
do
echo "the File is [$FILE]"
done

