简体   繁体   English

BASH监视文件的更好方法

[英]BASH better way to monitor files

I've made a Bash script to monitor some server log files for certain data and my method probably isn't the most efficient. 我已经制作了一个Bash脚本来监视某些数据的服务器日志文件,而我的方法可能不是最有效的。

One section specifically bugs me is that I have to write a newline to the monitored log so that the same line wont be read over continually. 有一部分特别让我感到烦恼的是,我必须在受监视的日志中写入换行符,以便同一行不会被连续读取。

Feedback would be greatly appreciated! 反馈将不胜感激!

#!/bin/bash

serverlog=/home/skay/NewWorld/server.log
onlinefile=/home/skay/website/log/online.log
offlinefile=/home/skay/website/log/offline.log
index=0

# Creating the file
if [ ! -f "$onlinefile" ]; then
    touch $onlinefile
    echo "Name                  Date            Time" >> "$onlinefile"
fi
if [ ! -f "$offlinefile" ]; then
    touch $offlinefile
    echo "Name                  Date            Time" >> "$offlinefile"
fi

# Functions
function readfile {

# Login Variables
loginplayer=`tail -1 $serverlog | grep "[INFO]" | grep "joined the game" | awk '{print $4}'`
logintime=`tail -1 $serverlog | grep "[INFO]" | grep "joined the game" | awk '{print $2}'`
logindate=`tail -1 $serverlog | grep "[INFO]" | grep "joined the game" | awk '{print $1}'`

# Logout Variables
logoutplayer=`tail -1 $serverlog | grep "[INFO]" | grep "left the game" | awk '{print $4}'`
logouttime=`tail -1 $serverlog | grep "[INFO]" | grep "left the game" | awk '{print $2}'`
logoutdate=`tail -1 $serverlog | grep "[INFO]" | grep "left the game" | awk '{print $1}'`

# Check for Player Login
    if [ ! -z "$loginplayer" ]; then
        echo "$loginplayer          $logindate  $logintime" >> "$onlinefile"
        echo "Player $loginplayer login detected" >> "$serverlog"
        line=`grep -rne "$loginplayer" $offlinefile | cut -d':' -f1`
        if [ "$line" > 1 ]; then
            sed -i "$line"d $offlinefile
            unset loginplayer
                    unset line
        fi
    fi
# Check for Player Logout
    if [ ! -z "$logoutplayer" ]; then
        echo "$logoutplayer         $logoutdate $logouttime" >> "$offlinefile"
        echo "Player $loginplayer logout detected" >> "$serverlog"
        line=`grep -rne "$logoutplayer" $onlinefile | cut -d':' -f1`
        if [ "$line" > 1 ]; then
            sed -i "$line"d $onlinefile
            unset logoutplayer
            unset line
        fi
    fi
}

# Loop
while [ $index -lt 100 ]; do
    readfile
done

Thanks! 谢谢!

instead of using multiple 而不是使用多个

tail -n 1 file

try the following construct: 尝试以下构造:

tail -f file | while read line;do
   echo "read: $line"
done

it will be much more reliable...and won't read the same line twice ;) 它将更加可靠...并且不会两次读取同一行;)

note: by using new processes of grep/awk/etc you are burning away processes...it's not that it is critical, but usually process creation is expensive...but if new lines occur rarely it's perfectly fine 注意:通过使用grep / awk / etc的新进程,您正在消耗进程...这不是很关键,但是通常进程创建是昂贵的...但是,如果很少出现新的行,那就太好了

where i want'ed to get is: if you are intrested, take a look at bash builting string manipulator function replace $(x/aa} ${x//aa} and friends..or try to use extended regexpes with grep 我想要得到的是:如果您很感兴趣,请看一下bash构建字符串操纵器函数,替换$(x / aa} $ {x // aa}和朋友..或尝试对grep使用扩展的正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM