在 bash 中列出目录路径中带有空格的文件时出现问题

发布于 2024-11-27 12:57:08 字数 615 浏览 1 评论 0原文

当在命令行中输入目录时,将显示以下内容:

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 \.png

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:

; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

似梦非梦 2024-12-04 12:57:08

另一种选择是更改 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

man bash 中了解有关 IFS 的更多信息。

Another option is to change 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.

叹沉浮 2024-12-04 12:57:08

使用更多引号不解析 ls 输出

for image in "$wdir"/**/*.png; do

Use more quotes and don't parse ls output.

for image in "$wdir"/**/*.png; do
枯寂 2024-12-04 12:57:08

如果您喜欢使用 while read 和由管道创建的子进程,您可以:

find . -name '*.png' | while read FILE
do 
    echo "the File is [$FILE]"
done

If you fine with using while read and subprocess created by pipe, you can:

find . -name '*.png' | while read FILE
do 
    echo "the File is [$FILE]"
done
洛阳烟雨空心柳 2024-12-04 12:57:08

您可以尝试用 [[:space:]] 代替空格

wdir="/Volumes/Development/My[[:space:]]Project/Project"

或执行命令来转换单个空格

wdir=`echo "$wdir" | sed 's/[[:space:]]/\[[:space:]]/g'`

you can try, [[:space:]] in place of space

wdir="/Volumes/Development/My[[:space:]]Project/Project"

or execute command to convert single space

wdir=`echo "$wdir" | sed 's/[[:space:]]/\[[:space:]]/g'`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文