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

Shell脚本写入Cron报错:find命令-exec参数缺失问题排查

问题诊断与修复

你碰到的find: missing argument to '-exec'错误,本质是shell对分号;的解析行为差异导致的。

当你直接在终端敲命令时,shell会把-exec ... {};里的;当成find命令的一部分。但把命令写到脚本里后,shell会把;当作命令分隔符——它会觉得find命令到{}就结束了,后面的;是下一个命令的开始,这就导致find没收到-exec要求的终止符,直接报错。

快速修复方案

只需要把-exec结尾的;转义成\;,让shell把它完整传递给find。修改后的脚本如下:

#!/bin/bash
# 修复第一个find的-exec终止符问题
find . -mmin -60 -name "*.jpg" $(printf "! -name %s " $(cat processed.txt)) ! -name cache -exec convert -resize 1000x800 -quality 85% {} {} \;
# 第二个find的-exec也建议规范转义分号
find . -mmin -60 -type f -name "*.jpg" -exec basename {} \; &> processed.txt

额外优化建议

如果你的processed.txt里有带空格或特殊字符的文件名,当前的$(cat processed.txt)写法会因为shell分词出问题。可以改用数组来安全处理:

#!/bin/bash
# 用数组存储排除规则,兼容带空格的文件名
exclude_patterns=()
while IFS= read -r file; do
    exclude_patterns+=("! -name" "$file")
done < processed.txt
exclude_patterns+=("! -name" "cache")

# 调用find时展开数组
find . -mmin -60 -name "*.jpg" "${exclude_patterns[@]}" -exec convert -resize 1000x800 -quality 85% {} {} \;
find . -mmin -60 -type f -name "*.jpg" -exec basename {} \; &> processed.txt

这种写法能避免文件名里的空格、换行或特殊字符导致的错误,更健壮。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:11:15