0

一部のサーバー ログ ファイルで特定のデータを監視する Bash スクリプトを作成しましたが、この方法はおそらく最も効率的ではありません。

特に私を悩ませているセクションの 1 つは、同じ行が継続的に読み取られないように、監視対象のログに改行を書き込む必要があることです。

フィードバックをお待ちしております。

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

ありがとう!

4

1 に答える 1