Search code examples
bashcurlftpwc

Script to monitor any modifications in files in my FTP site


Iam trying to build a script to monitor any modifications in files in my FTP site. The script is given below. I have used wc -l to count the no. of files in the directory and have defined the constant value of files if there is going to be any modification in files like if my co-worker updates in my FTP this will send me a notification. Am trying to cron this to achieve. But the script actually just hangs after the count . It doesn't provide me the expected result. Is there anything that am wrong with the code. Am just a beginner in Bash could anyone help me solve this

#!/usr/bin/bash
curl ftp://Sterst:abh89TbuOc@############################/Test/| wc -l ;
read b;
a=9
if [ "$b" != "$a" ];
then 
echo  "FTP dir has modified mail" -s "dir notification" sni912@######.com;
fi

Solution

  • A couple of notes about your code:

    #!/usr/bin/bash
    curl ftp://Sterst:abh89TbuOc@############################/Test/| wc -l ;
    read b;
    

    That does not do what you think it does: the wc output goes to stdout, not into the read command. Do this instead: b=$( curl ... | wc -l )

    a=9
    if [ "$b" != "$a" ];
    

    Since the wc output will have some extra whitespace, better to do a numeric comparison:
    if (( a != b ))

    then 
    echo  "FTP dir has modified mail" -s "dir notification" sni912@######.com;
    

    You probably mean this:

    echo  "FTP dir has modified" | mail -s "dir notification" sni912@######.com;
    

    I would write this:

    listing=$( curl ftp://... )
    num=$( echo "$listing" | wc -l )
    if (( num != 9 )); then
        mail -s "ftp dir modification" email@example.com <<END
    FTP directory modification
    
    $listing
    END
    fi