You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批量解压RAR文件时遇失败,如何让循环继续执行?

解决RAR解压脚本遇加密文件停滞的问题

你的脚本遇到加密RAR就停住,不是因为脚本退出,而是unrar在遇到加密文件时会进入交互模式等待输入密码,导致循环无法继续执行后续文件。加|| true没用,因为此时unrar并没有返回错误退出码,只是卡在输入环节。

修改方案是给unrar添加-p-参数(小写p加减号),这个参数会让unrar不提示输入密码,直接跳过加密文件:

find ./downloaded/ -type f -iname \*.rar | while read ss
do
    ss="$(realpath "$ss")"
    cd "$(dirname "$ss")"
    unrar x -r -o+ -p- "$ss" || true
done

另外可以优化的点:

  • 用find的-execdir简化目录切换逻辑,避免cd带来的潜在问题:
find ./downloaded/ -type f -iname \*.rar -execdir unrar x -r -o+ -p- {} \;
  • 如果需要记录加密文件路径,可把unrar输出定向到日志:
find ./downloaded/ -type f -iname \*.rar | while read ss
do
    ss="$(realpath "$ss")"
    cd "$(dirname "$ss")"
    unrar x -r -o+ -p- "$ss" 2>&1 | grep -q "password protected" && echo "加密文件:$ss" >> encrypted_rar.log || true
done

内容的提问来源于stack exchange,提问作者huangyingw

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 15:45:32