1

I've got a script that I run continually to monitor vsftpd logs. Here is a small example:

#!/bin/sh
tail -n0 -F /var/log/vsftpd.log | while read line; do
    if echo "$line" | grep -q 'OK UPLOAD:'; then
        username=$(echo "$line" | cut -d" " -f9 | sed 's/\[\(.*\)\]/\1/')
        if [ $? -ne 0 ]; then
            echo "ERROR: Failure to get parse username. Line it is trying to parse: $line" >>/var/log/ftpmonitor.log
            # We need to remove the file with any error so it doesn't linger
            rm -rf $home$filenamewithpath
            if [ $? -ne 0 ]; then
                echo "ERROR: Failed to delete video file" >>/var/log/ftpmonitor.log
                exit 1
            fi
            exit 1
        fi
     # lot of other stuff here...
     fi
done

I want to catch any errors I can with any command failure and stop. Originally I thought I'd use exit to stop. Normally that makes sense but in this case this script needs to keeping running all the time monitoring the vsftpd logs. So I don't want to exit the script I want to just stop any other commands after that failure. How can I achieve this?

1 Answer 1

3

I'm assuming you mean you want to skip to the next line of /var/log/vsftpd.log?
If so, just use continue.

#!/bin/sh
tail -n0 -F /var/log/vsftpd.log | while read line; do
    if echo "$line" | grep -q 'OK UPLOAD:'; then
        username=$(echo "$line" | cut -d" " -f9 | sed 's/\[\(.*\)\]/\1/')
        if [ $? -ne 0 ]; then
            echo "ERROR: Failure to get parse username. Line it is trying to parse: $line" >>/var/log/ftpmonitor.log
            # We need to remove the file with any error so it doesn't linger
            rm -rf $home$filenamewithpath
            if [ $? -ne 0 ]; then
                echo "ERROR: Failed to delete video file" >>/var/log/ftpmonitor.log
                continue
            fi
            continue
        fi
     # lot of other stuff here...
     fi
done

continue simply skips to the next iteration of the enclosing loop.

1
  • oh crap, of course. Thx.
    – Tom
    Apr 5, 2013 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .