Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if command response does not contain str

I have a script which works great... but I'd like it not do anything if there is no one online. I looked over all the documentation on how to check if a str is not within a variable, but it always thows the same error, which I assume may be something else, but literally nothing else is touched besides the addition of the fi at the bottom for the new if statement to check if there is no users online...

Error: startAnnouncement.sh: 44: [[: not found

#!/bin/bash

checkServer=$(/etc/init.d/minecraft status)
checkUsers=$(/etc/init.d/minecraft command list)
cd /.smc

# Is the server even running?
if [[ $checkServer =~ "is running" ]]; then

    #Is anyone online to even see the message...?
    if [[ ! $checkUsers =~ "are 0 out" ]]; then

       # No count file? Create it.
        if [ ! -f /.smc/lastAnnouncement.txt ]; then
            echo 0 > /.smc/lastAnnouncement.txt
        fi

        # Load count
        lastAnn=$(cat /.smc/lastAnnouncement.txt)

        # ANNOUNCEMENTS
        announcement[0]='Dont  forget to check out http://fb.com/pyrexiacraftfans for news and updates'
        announcement[1]='Use our Facebook page to request land protection! Visit http://fb.com/pyrexiacraftfans'
        announcement[2]='Stay tuned for the announcement of our spawn build competition soon on our Facebook page!'
        announcement[3]='We have upgraded the server with mcMMO and Essentials Economy! Stay tuned for shop openings!'

        announcementText=${announcement[$lastAnn]}

        # Send announcement
        sendAnnouncement=$(/etc/init.d/minecraft command say $announcementText)

        # Next announcement count
        ((++lastAnn))

        # Write next announacment count
        # Should we restart announcement que?
        if [ $lastAnn -gt $((${#announcement[@]}-1)) ]; then
                echo 0 > /.smc/lastAnnouncement.txt
        else
                echo $lastAnn > /.smc/lastAnnouncement.txt
        fi

    fi

fi
like image 765
WASasquatch Avatar asked Dec 16 '22 09:12

WASasquatch


1 Answers

Your conditions don't actually use regex-special chars,

if [[ $checkServer =~ "is running" ]]; then
    if [[ ! $checkUsers =~ "are 0 out" ]]; then

Also, you need to use !~ operator on the 2nd condition: you have too many arguments

So you might as well use glob patterns.

if [[ $checkServer == *"is running"* ]]; then
    if [[ $checkUsers != *"are 0 out"* ]]; then
like image 132
glenn jackman Avatar answered Jan 05 '23 12:01

glenn jackman