I would like to test a file for a string and an array of strings. My problem is the order of things.
If there is a line "Alice was shot by Bob" my code calls both players dead even if Bob is still alive. So I only want to test for "${player} ${deaths}" and ignore any "${player}" after ${deaths}.
An example line from the log file:
18:45:23 [Server/Thread][INFO] Alice was shot by Bob using weapon
The code should recognise "Alice" and "was shot by" but not "Bob" because "Bob" is after the death message. If there is only a death message or a player name it should do nothing which it currently does. It should also ignore the "using weapon" and the "server stuff" before Alice.
This is what I got so far:
#!/bin/bash
# testing for death messages and performing separate actions for each player
screenlog="screen.log" # this is a growing logfile
tmpscreenlog="tmpscreen.log" # this is always the last line of screenlog
player01="Alice" # the first player
player02="Bob" # the second player
deaths=( # an array of possible death messages
"was shot by"
"burned to death"
"starved to death"
"drowned"
)
while true; do
tail -n1 ${screenlog} >> ${tmpscreenlog} # this line creates a one line buffer from the growing screenlog file
if [[ ! -z $(grep "${player01}\|${deaths[*]}" "${tmpscreenlog}") ]]; then # if Alice and any death occurs in tmpscreen.log
echo "Alice is dead!" # output Alices death and perform some commands
screen -Rd sessionname -X stuff "ban ${player01} You died! Thank you for participating.$(printf '\r')"
# commands for Alice
fi
if [[ ! -z $(grep "${player02}\|${deaths[*]}" "${tmpscreenlog}") ]]; then # if Bob and any death occurs in tmpscreen.log
echo "Bob is dead!" # output Bobs death and perform some commands
screen -Rd sessionname -X stuff "ban ${player02} You died! Thank you for participating.$(printf '\r')"
# commands for Bob
fi
rm ${tmpscreenlog} # this line removes the one line screenlog buffer
sleep 1s
done
Thank you for any suggestions and help <3