Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 (( ; ; ))

#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
        #if the battery IS NOT present, run this code
        sendemail -f -t 214******* -u NTA TV Alert -m "The battery from the computer is either missing, or removed. Please check ASAP." -s -o tls=yes -xu -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 -t 214******* -u NTA TV Alert -m "The battery from the computer is charging. This MIGHT mean that something just happened" -s -o tls=yes -xu -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 -t 214******* -u NTA TV Alert -m "The battery from the computer is discharging. This shouldn't be happening. Please check ASAP." -s -o tls=yes -xu -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


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:

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

is the error something super simple like a forgotten semicolon?

share|improve this question

A few problems here:

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

if <condition>
elif <other-condition>

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
    # code to execute if battery is present
    # code to execute if battery is not present

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' ]
    # code to execute when $somevar equals 'some-string'

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

Your Answer


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.