尝试RM时如何处理丢失的操作数错误

时间:2018-12-30 05:03:11

标签: bash find rm

我有以下代码

([number], [pixels])

如果没有要删除的文件,那么我会避免出现“缺少操作数”错误,因为它会触发脚本的出口1。

如果我有以下代码

rm "$torrent"/*.{txt,nfo,jpg} && echo "removed \"txt,nfo,jpg\" files"

rm -f "$torrent"/*.{txt,nfo,jpg} && echo "removed \"txt,nfo,jpg\" files" 始终计算为true,即使没有删除任何文件。

如果某些文件被删除,我想显示消息“已删除..文件”。如果没有删除任何文件,我希望保持静音并且不会引发任何错误。

我玩弄

rm

,但是即使没有删除任何文件,它始终评估为true。

1 个答案:

答案 0 :(得分:1)

只需将其重写为正确的if语句:

if rm "$torrent"/*.{txt,nfo,jpg}
then
  echo "removed \"txt,nfo,jpg\" files"
if

这使您可以在set -e有效时对退出状态做出反应,而无需退出脚本