使用 bash 检查包是否早于 24 小时

use*_*656 3 bash shell

我想检查我的最后一个文件是否早于 24 小时。(django 中的项目)我的目录中有很多 zip 包,所以我必须用这部分代码“过滤”最后一个:ls -1 | sort -n | tail -n1

我在 .sh 文件中的代码:

#!/bin/bash

file="$HOME/path_directory ls -1 | sort -n | tail -n1"
current=`date +%s`;
last_modified=`stat -c "%Y" $file`;


if [ $(($current-$last_modified)) -gt 86400 ]; then
     echo "File is older that 24 hours" | mailx noreply@address -s "Older than 24 hours" me@mailmail.com
else
     echo "File is up to date.";
fi;
Run Code Online (Sandbox Code Playgroud)

这是我得到的一个错误:

stat: invalid option -- '1'
Try 'stat --help' for more information.
/path_directory/imported_file.sh: line 9: 1538734802-: syntax error: operand expected (error token is "-")
Run Code Online (Sandbox Code Playgroud)

如果有人做了类似的事情,请提示。

小智 5

我建议你尝试这个:

if test "`find file -mtime +1`"
Run Code Online (Sandbox Code Playgroud)

但如果你坚持的话,你可以通过将其更改为以下方式来修复它:

#!/bin/bash
file="$HOME/path_directory ls -1 | sort -n | tail -n1"
current=$(date +%s);
last_modified=$(stat -c "%Y" $file);

if [ $((current - last_modified)) -gt 86400 ]; then
     echo "File is older that 24 hours" | mailx noreply@address -s "Older than 24 hours" me@mailmail.com
else
     echo "File is up to date.";
fi;
Run Code Online (Sandbox Code Playgroud)