目录名中的空格导致错误

发布于 2024-12-26 04:19:49 字数 629 浏览 0 评论 0原文

您好,我有许多目录的名称中包含空格。我有一些脚本要在它们上运行。 这是一个示例脚本

#!/bin/bash
docid='/home/deel/PDF/rant/spr 2008 Shree rose Visheshank'
p=1
while [ $p -lt 117 ]; do
cp $docid/$p.pdf ./
p=$p+1
done

执行上述脚本后,我收到以下错误

cp: cannot stat `/home/deel/PDF/rant/spr': No such file or directory
cp: cannot stat `2008': No such file or directory
cp: cannot stat `Shree': No such file or directory
cp: cannot stat `rose': No such file or directory
cp: cannot stat `Visheshank/1.pdf': No such file or directory
./d2.sh: line 5: [: 1+1: integer expression expected

我尝试使用双引号“”,但结果是相同的。 我应该做什么改变?

Hi I have many directories which have blank space in their names.I have some scripts to be run on them.
Here is an example script

#!/bin/bash
docid='/home/deel/PDF/rant/spr 2008 Shree rose Visheshank'
p=1
while [ $p -lt 117 ]; do
cp $docid/$p.pdf ./
p=$p+1
done

Upon executing the above script I get following errors

cp: cannot stat `/home/deel/PDF/rant/spr': No such file or directory
cp: cannot stat `2008': No such file or directory
cp: cannot stat `Shree': No such file or directory
cp: cannot stat `rose': No such file or directory
cp: cannot stat `Visheshank/1.pdf': No such file or directory
./d2.sh: line 5: [: 1+1: integer expression expected

I have tried using double quotes "" but the results are same.
What changes should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

沧桑㈠ 2025-01-02 04:19:49

这一行:

cp $docid/$p.pdf ./

应为:

cp "$docid/$p.pdf" ./

示例:

$ a='abc def'
$ ls $a
ls: abc: No such file or directory
ls: def: No such file or directory
$ ls "$a"
ls: abc def: No such file or directory

算术如下:

p=$[p+1]

示例:

$ p=1
$ q=$[p+1]
$ echo $p $q
1 2

This line:

cp $docid/$p.pdf ./

should read:

cp "$docid/$p.pdf" ./

Example:

$ a='abc def'
$ ls $a
ls: abc: No such file or directory
ls: def: No such file or directory
$ ls "$a"
ls: abc def: No such file or directory

And arithmetic goes like:

p=$[p+1]

Example:

$ p=1
$ q=$[p+1]
$ echo $p $q
1 2
高冷爸爸 2025-01-02 04:19:49

您需要用双引号引用包含空格的名称。此外,p=$p+1 并没有达到您的预期。这是更正后的循环:

for p in `seq 116`; do
    cp "$docid/$p.pdf" ./
done

You need to quote the name containing spaces with double quotes. Moreover, p=$p+1 does not do what you expect. Here is the corrected loop:

for p in `seq 116`; do
    cp "$docid/$p.pdf" ./
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文