gpt4 book ai didi

bash - 有没有机会在第一个错误时停止chown?

转载 作者:行者123 更新时间:2023-12-03 07:50:03 25 4
gpt4 key购买 nike

当我使用递归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”。
可能吗?

最佳答案

否。检测到此类错误的唯一方法是观看标准错误,但是到您可以检测到错误并对错误使用react时,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中都不存在这样的选项。)

关于bash - 有没有机会在第一个错误时停止chown?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32055841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com