查找返回“查找:.:权限被拒绝”,但我没有在其中搜索

发布于 2024-11-04 01:15:49 字数 520 浏览 0 评论 0原文

我有一个巨大的 shell 脚本,正在对其进行故障排除。我经常使用 sudo 从我的主目录运行脚本。每当执行 find 时,我都会看到以下错误:

find: .: Permission returned

确实,root 无权访问我的主目录(这是当前工作目录)目录或上面错误中的 . ),但我并没有要求 find 在我的主目录中执行任何操作,而是希望它完全不理会它。

为了真正说明这一点,我运行了以下命令:

sudo find /dev -maxdepth 1 -type f

,但仍然遇到相同的错误。如果删除 -type -f ,则会将错误附加到预期结果的末尾。当然,如果我 cd /dev 就没有错误......可能是因为 root 可以访问 /dev。尽管我不认为这会造成问题,但它使脚本看起来有问题。如何防止脚本显示这些错误?

I have an enormous shell script that I am troubleshooting. I often run the script from my home directory with a sudo. Whenever a find is executed, I see this error:

find: .: Permission denied

It is true that root does not have access to my home directory (which is the current working directory or . in the error above), but I'm not asking find to do anything in my home directory and would rather it leave it alone entirely.

To really drive the point home I ran this:

sudo find /dev -maxdepth 1 -type f

and still get the same error. If the -type -f is removed the error is appended to the end of the expected results. Of course, if I cd /dev there is no error..probably since root has access to /dev. Even though I don't think it's causing problems, it makes the script look buggy. How can I prevent the script from showing these errors?

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

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

发布评论

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

评论(3

撩动你心 2024-11-11 01:15:49

我在 GNU/Linux (Ubuntu) 上运行

strace find /dev -maxdepth 1

,结果发现 find 使用 fchdir 系统调用来遍历目录树,最后执行 fchdir返回到原来的工作目录。这是一个片段:

open(".", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW) = 4
fchdir(4)                               = 0

... irrelevant ...

write(1, "/dev\n", 5)                   = 5
open("/dev", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 5
fcntl64(5, F_GETFD)                     = 0x1 (flags FD_CLOEXEC)
fchdir(5)                               = 0

... potentially more fchdirs ...

fchdir(4)                               = 0
close(4)                                = 0

我的提示?在运行 find 之前,cd /tmp(或其他完全可访问的目录)。

I ran:

strace find /dev -maxdepth 1

on GNU/Linux (Ubuntu) and it turns out that find uses fchdir syscall to traverse the directory tree and finally executes fchdir to go back to the original working directory. Here's a snippet:

open(".", O_RDONLY|O_NOCTTY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_NOFOLLOW) = 4
fchdir(4)                               = 0

... irrelevant ...

write(1, "/dev\n", 5)                   = 5
open("/dev", O_RDONLY|O_NONBLOCK|O_LARGEFILE|O_DIRECTORY|O_CLOEXEC) = 5
fcntl64(5, F_GETFD)                     = 0x1 (flags FD_CLOEXEC)
fchdir(5)                               = 0

... potentially more fchdirs ...

fchdir(4)                               = 0
close(4)                                = 0

My hint? cd /tmp (or some other fully accessible directory) before running find.

深海蓝天 2024-11-11 01:15:49

cd / 添加到脚本的开头。除非您source它,否则该脚本将在子shell中运行,因此您自己的$PWD不会更改。如果您确实获取它,请在开头存储$PWD,并在结尾存储cd -- "$PWD",或者简单地cd - 如果您不在脚本中执行任何其他 cd 操作。

Add a cd / to the start of the script. Unless you source it, the script is run in a sub-shell, so your own $PWD will not be changed. If you do source it, either store $PWD at the start and cd -- "$PWD" at the end, or simply cd - if you don't do any other cds in the script.

·深蓝 2024-11-11 01:15:49

尝试重定向 stderr。例如,你可以把它扔掉:

find /dev 2>/dev/null

Try redirecting stderr. For example, you could throw it away:

find /dev 2>/dev/null
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文