Aaron Henderson
Aaron Henderson

Reputation: 191

BASH If [[ "$a" == *"$b"* ]] Always true even when it shouldn't be

I am trying to write a small bash script to monitor the output of RiotShield (a 3rd party player scraper for League of Legends) for crashes. If a keyword is found in the log it should kill the process and restart it.

Here is my bash script as is:

#!/bin/bash
crash[1]="disconnected"
crash[2]="38290209"
while true; do
    list=$(tail log.log)
    #clear
    echo "Reading Log"
    echo "========================================"
    echo $list
    for item in ${list//\\n/ }
    do
            for index in 1 2
            do
                    c=${crash[index]}
                    #echo "Crash Word:" $c
                    if [[ "$c" == *"$item"* ]]; then
                            echo "RiotShield has crashed."
                            echo "Killing RiotShield."
                            kill $(ps aux | grep '[R]iotShield.exe' | awk '{print $2}')
                            echo "RiotShield killed!"
                            echo "Clearing log."
                            echo > log.log
                            echo "Starting RiotShield"
                            (mono RiotShield.exe >> log.log &)

                    fi
            done

    done

    sleep 10
done

My crash array are keywords that I know show in the log when it crashes. I have 38290209 in there only for testing purposes as it is my summoner ID on League of Legends and the moment I preform a search for my Summoner name the ID shows in the log.

The problem is even when disconnected and 38290209 do not show up in the log my

if [[ "$c" == *"$item"* ]]; then

fires, kills the RiotShield process and then relaunches it.

The length of the crash array will grow as I find more keywords for crashes so I cant just do

if [[ "$c" == "*disconnected*" ]]; then

Please and thanks SOF

EDIT:

Adding working code:

#!/bin/bash
crash[1]="disconnected"
crash[2]="error"
while true; do
    list=$(tail log.log)
    clear
    echo "Reading Log"
    echo "========================================"
    echo $list
    for index in 1 2
    do
            c=${crash[index]}
            #echo "Crash Word:" $c
            if [[ $list == *$c* ]]; then
                    echo "RiotShield has crashed."
                    echo "Crash Flag: " $c
                    echo "Killing RiotShield."
                    kill $(ps aux | grep '[R]iotShield.exe' | awk '{print $2}')
                    echo "RiotShield killed!"
                    echo "Clearing log."
                    echo > log.log
                    echo "Starting RiotShield"
                    (mono RiotShield.exe >> log.log &)
                    fi
            done
    sleep 10
done

Upvotes: 4

Views: 945

Answers (1)

dogbane
dogbane

Reputation: 274738

I think you have the operands in your expression the wrong way around. It should be:

if [[ $item == *$c* ]]; then

because you want to see if a keyword ($c) is present in the line ($item).

Also, I'm not sure why you need to break the line into items by doing this: ${list//\\n/ }. You can just match the whole line.

Also note that double-quotes are not required within [[.

Upvotes: 5

Related Questions