0

I am trying to write a script to monitor some battery statuses on a laptop running as a server. To accomplish this, I have already started to write this code:

#! /bin/bash
# A script to monitor battery statuses and send out email notifications

#take care of looping the script
for (( ; ; ))
do

#First, we check to see if the battery is present...
if(cat /proc/acpi/battery/BAT0/state | grep 'present: *' == present:                 yes)
    {
        #Code to execute if battery IS present

        #No script needed for our application
        #you may add scripts to run
    }
else
    {
        #if the battery IS NOT present, run this code
        sendemail -f [email protected] -t 214*******@txt.att.net -u NTA TV Alert -m "The battery from the computer is either missing, or removed. Please check ASAP." -s smtp.gmail.com -o tls=yes -xu [email protected] -xp ***********
    }



#Second, we check into the current state of the battery
if(cat /proc/acpi/battery/BAT0/state | grep 'charging state: *' == 'charging state:                     charging')
    {
        #Code to execute if battery is charging
        sendemail -f [email protected] -t 214*******@txt.att.net -u NTA TV Alert -m "The battery from the computer is charging. This MIGHT mean that something just happened" -s smtp.gmail.com -o tls=yes -xu [email protected] -xp ***********
    }
#If it isn't charging, is it discharging?
else if(cat /proc/acpi/battery/BAT0/state | grep 'charging state: *' == 'charging     state:                 discharging')
    {
        #Code to run if the battery is discharging
        sendemail -f [email protected] -t 214*******@txt.att.net -u NTA TV Alert -m "The battery from the computer is discharging. This shouldn't be happening. Please check ASAP." -s smtp.gmail.com -o tls=yes -xu [email protected] -xp ***********
    }
#If it isn't charging or discharging, is it charged?
else if(cat /proc/acpi/battery/BAT0/state | grep 'charging state: *' == 'charging state:                 charged')
    {
        #Code to run if battery is charged
    }



done

I'm pretty sure that most of the other stuff works correctly, but I haven't been able to try it because it will not run. whenever I try and run the script, this is the error that I get:

./BatMon.sh: line 15: syntax error near unexpected token `}'
./BatMon.sh: `      }'

is the error something super simple like a forgotten semicolon?

1 Answer 1

3

A few problems here:

Firstly, that's not how you write if/else statements in bash. Instead, you need something like:

if <condition>
then
    <action>
elif <other-condition>
then
    <other-action>
else
    <another-action>
fi

Secondly, the condition you're checking here will not work; the if statement will check the return value of condition. So, you need the condition to be a command (or pipeline of commands) that will return a zero or non-zero exit status.

So, try something like:

if grep 'present:.*yes' /proc/acpi/battery/BAT0/state
then
    # code to execute if battery is present
else
    # code to execute if battery is not present
fi

In this case, the grep will succeed (ie, return a zero exit status) if the BAT0/state file matches the pattern present:.*yes.

If you need to do string matching, you need to use the [ command with an = operator:

if [ "$somevar" = 'some-string' ]
then
    # code to execute when $somevar equals 'some-string'
fi

For more information on if-statements in bash, see help for if:

help if

Or, see the bash manpage for general bash programming info:

man bash

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .