Bash脚本检查ftp上的实际日期文件

问题描述 投票:0回答:1

我有简单的脚本:

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服务器并向我发送电子邮件?

bash ftp lftp
1个回答
0
投票

我认为这里有三个可能的问题。第一个是date | awk '{print $2" "$3}'在当天结束时包含一个逗号。第二个是天数由lsdate填补空间,但是awk将剥离填充物。第三是ifbash的终结者是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

© www.soinside.com 2019 - 2024. All rights reserved.