查找返回“查找:.:权限被拒绝”,但我没有在其中搜索
我有一个巨大的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在 GNU/Linux (Ubuntu) 上运行
,结果发现
find
使用fchdir
系统调用来遍历目录树,最后执行fchdir
返回到原来的工作目录。这是一个片段:我的提示?在运行
find
之前,cd /tmp
(或其他完全可访问的目录)。I ran:
on GNU/Linux (Ubuntu) and it turns out that
find
usesfchdir
syscall to traverse the directory tree and finally executesfchdir
to go back to the original working directory. Here's a snippet:My hint?
cd /tmp
(or some other fully accessible directory) before runningfind
.将
cd /
添加到脚本的开头。除非您source
它,否则该脚本将在子shell中运行,因此您自己的$PWD
不会更改。如果您确实获取它,请在开头存储$PWD
,并在结尾存储cd -- "$PWD"
,或者简单地cd -
如果您不在脚本中执行任何其他cd
操作。Add a
cd /
to the start of the script. Unless yousource
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 andcd -- "$PWD"
at the end, or simplycd -
if you don't do any othercd
s in the script.尝试重定向 stderr。例如,你可以把它扔掉:
Try redirecting stderr. For example, you could throw it away: