0

我正在寻找的是 Minecraft 日志中的玩家死亡消息。

因此,我的脚本正在查看 Minecraft server.log,并且我创建了一个包含所有可能死亡消息的数组,但它无法正常工作,因为它会输出日志中出现的任何行。

#!/bin/bash

serverlog=/home/skay/NewWorld/server.log
outputfile=/home/skay/website/log/playerstats.log


##      File Creation!
if [ ! -f "$outputfile" ]; then
    touch $outputfile
    echo "file created"; else
    echo "file already existed"
fi

echo "Starting Loop"

while true; do

index="0"
newline=`tail -n 1 "$serverlog"`
joined=`tail -n 1 $serverlog | grep "[INFO]" | grep "joined the game"`
left=`tail -n 1 $serverlog | grep "[INFO]" | grep "left the game"`

##      Death Message Array
deathmsg=(  "was squashed by a falling anvil"
            "was pricked to death"
            "walked into a cactus whilst trying to escape"
            "was shot by arrow"
            "drowned"
            "blew up"
            "was blown up by"
            "hit the ground too hard"
            "fell from a high place"
            "fell off a ladder"
            "fell off some vines"
            "fell out of the water"
            "fell into a patch of fire"
            "fell into a patch of cacti"
            "was doomed to fall"
            "was shot off some vines by"
            "was shot off a ladder by"
            "was blown from a high place by"
            "went up in flames"
            "burned to death"
            "was burnt to a crisp whilst fighting"
            "walked into a fire whilst fighting"
            "was slain by"
            "was shot by"
            "was fireballed by"
            "was killed by"
            "got finished off by"
            "was slain by"
            "tried to swim in lava"
            "died"
            "got finished off by"
            "was slain by"
            "was shot by"
            "was killed by"
            "was killed by magic"
            "starved to death"
            "suffocated in a wall"
            "was killed while trying to hurt"
            "fell out of the world"
            "fell from a high place and fell out of the world"
            "was knocked into the void by"
            "withered away")


##      Player Joined
    if [ "$newline" != "$oldline" ]; then
        if [ "$newline" == "$joined" ]; then
            echo "$joined"
            oldline="$newline"
        fi

##      Player Disconnected
        if [ "$newline" == "$left" ]; then
            echo "$left"
            oldline="$newline"
        fi

##      Player Death Message
        while [ "$index" -le "42" ]; do 
            death=`tail -n 1 $serverlog | grep "[INFO]" | grep "${deathmsg[$index]}"`
            if [ "$newline" == "$death" ]; then
                echo "$death"
                oldline="$death"
            fi
            index=$[$index+1]
        done
    fi
done

server.log 示例:

2013-07-21 00:38:36 [SEVERE] Reached end of stream for /79.97.91.46
2013-07-21 00:38:38 [INFO] Fenlig[/79.97.91.46:59709] logged in with entity id 880461 at (541.34081297678, 48.0, 463.3734054913931)
2013-07-21 00:38:38 [INFO] Fenlig joined the game
Player Fenlig login detected
2013-07-21 00:39:49 [INFO] Fenlig was doomed to fall
2013-07-21 00:39:57 [INFO] Fenlig lost connection: disconnect.quitting
2013-07-21 00:39:57 [INFO] Fenlig left the game
4

2 回答 2

1

@Andy 答案是更好的方法;你也可以用 bash 做类似的事情。

但是要直接回答这个问题,我认为您的问题是您的索引达到了 42,并且

$ echo ${deathmsg[42]}

$

由于数组索引以 0 开头,以 41 结尾。42 为空白,然后匹配 death= 行上的最后一个 grep。

于 2013-07-21T03:07:15.490 回答
0

你有没有想过使用awk?将其写入 parse_log.awk

{
  /* only process [INFO] messages */
  if ($3 == "[INFO]") {
    rest_of_string = substr($0, index($0, $4));

    /* logged in, and anything else you want to skip ... */
    if ($5 == "logged") {
      next;
    }

    /* joined ... */
    if ($5 == "joined") {
      print rest_of_string;
      next;
    }

    /* left ... */
    if ($5 == "left") {
      print rest_of_string;
      next;
    }

    /* otherwise death */
    print rest_of_string;
  }
}

然后在命令行上你可以简单地运行这个

$ tail -n 1 -f /home/skay/website/log/playerstats.log | awk -f parse_log.awk >> /home/skay/website/log/playerstats.log

本质上,tail 一直跟踪日志(-f),然后将其通过管道传输到您的 awk 程序,如果它产生输出,则将其附加到您的文件中

于 2013-07-21T03:00:24.180 回答