我已经编写了这个脚本,但是输出不正确。它返回 stat 无法统计没有这样的文件或目录。文件格式为Living Room-20180418-0955588134.jpg
任何帮助将不胜感激。
#!/bin/sh
LASTFILE=$(cd /volume1/surveillance/@Snapshot && ls *.jpg | tail -1)
# Input file
# How many seconds before file is deemed "older"
OLDTIME=3600
# Get current and file times
CURTIME=$(date +%s)
FILETIME=$(stat "$LASTFILE" -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)
# Check if file older
if [ $TIMEDIFF -gt $OLDTIME ]; then
echo "No Movement Dectected in Last Hour" ;
exit 1
fi
答案1
使用 GNUfind
或兼容:
if
! find /volume1/surveillance/@Snapshot -name '*.jpg' -mmin -60 |
grep -q '^'
then
echo No movement detected in the last hour
exit 1
fi
或者与zsh
:
last_hour=(/volume1/surveillance/@Snapshot/*.jpg(Nmh-1))
if (($#last_hour = 0)); then
echo No movement detected in the last hour
exit 1
fi
答案2
原因是因为“stat”看不到完整路径“/volume1/surveillance/@Snapshot/”。它只看到文件名。所以需要修改脚本。
#!/bin/sh
DIR=/volume1/surveillance/@Snapshot
LASTFILE=$(cd $DIR && ls *.jpg | tail -1)
# Input file
# How many seconds before file is deemed "older"
OLDTIME=3600
# Get current and file times
CURTIME=$(date +%s)
FILETIME=$(stat $DIR/$LASTFILE -c %Y)
TIMEDIFF=$(expr $CURTIME - $FILETIME)
# Check if file older
if [ $TIMEDIFF -gt $OLDTIME ]; then
echo "No Movement Dectected in Last Hour" ;
exit 1
fi