Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
oh crap, of course. Thx. –  Tom Apr 5 '13 at 20:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.