我有一个不断运行的脚本来监视 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
我想捕获任何命令失败的错误并停止。原本我以为我会使用 exit 来停止。通常这是有道理的,但在这种情况下,该脚本需要一直运行以监视 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
只是跳到封闭循环的下一次迭代。