我有简单的脚本:
lftp user@server -e "cd dir && ls -ltr ;exit" > list.txt
if files=$(cat list.txt | grep "`date | awk '{print $2" "$3}'`") ; then
echo "$files" | mailx -s "File exists" [email protected]
else
echo "$files" | mailx -s "File not exists" [email protected]
end if
问题是因为这个grep日期来自文件不能正常工作,有时候工作正常。
有人可以告诉我,如果实际日期文件存在,有什么更好的方法来检查ftp服务器并向我发送电子邮件?
我认为这里有三个可能的问题。第一个是date | awk '{print $2" "$3}'
在当天结束时包含一个逗号。第二个是天数由ls
和date
填补空间,但是awk
将剥离填充物。第三是if
中bash
的终结者是fi
而不是end if
。
尝试
if files=$(grep "`date '+%b %e'`" list.txt); then
echo "$files" | mailx -s "File exists" [email protected]
else
echo "$files" | mailx -s "File not exists" [email protected]
fi
%e
中的date
假设ls
返回一个空间填充日数。如果您的系统使用零填充日期编号,请尝试%d
。