Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 my-email@gmail.com -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 my-email@gmail.com -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 my-email@gmail.com -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 my-email@gmail.com -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 my-email@gmail.com -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 my-email@gmail.com -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?

share|improve this question
add comment

1 Answer

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
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.