Search code examples
bashftplftp

Bash script check actually date files on ftp


i have simple script :

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"  name@name.com
else
    echo "$files" | mailx -s "File not exists" name@name.com
end if

problem is because this grep date from file not working corectly, sometimes is working.

Can someone tell me what is better way to check on ftp server if actual date file exists and send me email?


Solution

  • I think there are three possible problems here. The first is that date | awk '{print $2" "$3}' includes a comma on the end of the day number. The second is that the day number is space padded by ls and date, but awk will be stripping the padding. The third is that the terminator for if in bash is fi rather then end if.

    Try

    if files=$(grep "`date '+%b %e'`" list.txt); then
         echo "$files" | mailx -s "File exists"  name@name.com
    else
        echo "$files" | mailx -s "File not exists" name@name.com
    fi
    

    The %e in date assumes ls returns a space-padded day number. If your system uses a zero-padded day number, try %d.