Bash 如果使用“查找”未找到任何结果,则打印错误消息

Bash 如果使用“查找”未找到任何结果,则打印错误消息,bash,shell,unix,xargs,Bash,Shell,Unix,Xargs,我试图编写一个bash脚本,打印路径$1中所有在$2之后修改过的文件。这是我的剧本: find ./$1 -mtime -$2 -type f | xargs du -h | sort 现在,如果脚本没有返回任何结果,我希望它打印一条错误消息,例如“找不到这样的文件”。如果且仅当未找到并打印任何文件名时,如何打印邮件 提前感谢。您可以使用: #!/bin/bash # create a temp file tmp=$(mktemp) # run find and redirect outp

我试图编写一个bash脚本,打印路径$1中所有在$2之后修改过的文件。这是我的剧本:

find ./$1 -mtime -$2 -type f | xargs du -h | sort
现在,如果脚本没有返回任何结果,我希望它打印一条错误消息,例如“找不到这样的文件”。如果且仅当未找到并打印任何文件名时,如何打印邮件

提前感谢。

您可以使用:

#!/bin/bash

# create a temp file
tmp=$(mktemp)

# run find and redirect output to temp file
find ./"$1" -mtime -"$2" -type f > "$tmp"

# check if temp file is not empty 
if [[ -s "$tmp" ]]; then
    cat "$tmp"
else
    echo "No such files found"
fi

# delete temp file
rm "$tmp"

您可以存储结果并检查变量是否有内容;如果没有,则打印此错误;如果找到,则打印输出:
[-z“$result”]&&echo“未找到任何内容”| | echo“$result”
我们可以将整个输出存储在局部变量中,并检查打印实际结果与打印
时是否未找到此类文件。
将其存储在文件中不是更好吗?是的,我同意使用临时文件比局部变量更好。