【问题标题】:Is there any opportunity to stop chown on first error?有没有机会在第一个错误时停止 chown ?
【发布时间】:2015-08-17 16:56:34
【问题描述】:

当我使用递归 chown 时,我想在第一个错误时停止它。

$ chown -R /tmp/cache someuser
chown: changing ownership of `/tmp/cache/1/thumbnail/100x/9df78eab33525d08d6e5fb8d27136e95/h/d': Operation not permitted
chown: changing ownership of `/tmp/cache/1/thumbnail/100x/9df78eab33525d08d6e5fb8d27136e95/h': Operation not permitted
chown: changing ownership of `/tmp/cache/1/thumbnail/100x/9df78eab33525d08d6e5fb8d27136e95': Operation not permitted
chown: changing ownership of `/tmp/cache/1/thumbnail/100x': Operation not permitted
chown: changing ownership of `/tmp/cache/1/thumbnail': Operation not permitted
chown: changing ownership of `/tmp/cache/1': Operation not permitted
chown: changing ownership of `/tmp/cache': Operation not permitted

即我应该只得到echo $? 的第一个错误和状态代码“1”。
有可能吗?

【问题讨论】:

  • 我投票决定将此问题作为题外话结束,因为它不是编程问题,最好在 unix.stackexchange.com 或 superuser.com 上提问
  • 你忘了-R吗?
  • 简而言之,不;我认为没有一种(标准)方法可以让chown -R 在第一个错误时停止。

标签: bash error-handling chown


【解决方案1】:

没有。检测此类错误的唯一方法是观察标准错误,但是当您可以检测到错误并做出反应时,chown 将继续前进。如果您需要在任何错误后采取措施,您需要对每个文件分别运行chown,如果出现错误则采取措施。

# Assuming bash 4 for simplicity; handling the recursive directory walk
# without ** is left as an exercise for the reader.
shopt -s globstar
for f in /tmp/cache/**/*; do
    chown "$f" someuser || break
done

(完全有可能有人可以实现chown,并带有在第一个错误后停止的选项,但在 POSIX 规范或 GNU chown 中都不存在这样的选项.)

【讨论】:

    猜你喜欢
    • 2012-11-09
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 2016-07-12
    • 2022-10-13
    相关资源
    最近更新 更多