종료하지 않고 쉘 스크립트에서 처리를 중지합니다.

종료하지 않고 쉘 스크립트에서 처리를 중지합니다.

vsftpd 로그를 모니터링하기 위해 지속적으로 실행되는 스크립트가 있습니다. 다음은 작은 예입니다.

#!/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

명령 실패 시 오류를 포착하고 중지하고 싶습니다. 원래는 종료를 사용하여 중지할 것이라고 생각했습니다. 일반적으로 이는 의미가 있지만 이 경우 vsftpd 로그를 모니터링하려면 스크립트가 항상 실행되어야 합니다. 따라서 스크립트를 종료하고 싶지 않고 실패한 후에 다른 명령을 중지하고 싶습니다. 이 목표를 어떻게 달성할 수 있나요?

답변1

다음 줄로 넘어가고 싶다는 뜻인 것 같은데요 /var/log/vsftpd.log?
그렇다면 그냥 사용하세요 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둘러싸는 루프의 다음 반복으로 점프하면 됩니다.

관련 정보