1

I am trying to make a script that will conntect to a ups and get the battery value and store it in a variable.

I have managed to connect to the ups and get infomation from it but I am strugling to get just the battery value into a variable.

Script so far

#!/bin/bash
# 

# Connect to ups and output values into output var
output="$( upsc ups@localhost)"

# Trip the output to just have battery charge info
output1="$( sed -n '/battery.charge:*/,/./p' <<< "$output" )"

echo "$output1";

Output returns

ambient.1.humidity.alarm.high: 60.00
ambient.1.humidity.alarm.low: 30.00
ambient.1.temperature.alarm.high: 40.00
ambient.1.temperature.alarm.low: 10.00
battery.charge: 100.00
battery.current: 0.00
battery.current.total: 0.00
battery.date: 03/15/2013
battery.runtime: 2040.00
battery.runtime.low: 120
battery.voltage: 54.70
device.mfr: APC
device.model: Smart-UPS 2200
device.serial: IS1248007101
device.type: ups
driver.name: snmp-ups
driver.parameter.pollinterval: 2
driver.parameter.port: 172.16.27.207
driver.version: 2.7.2
driver.version.data: apcc MIB 1.2
driver.version.internal: 0.72
input.frequency: 50.00
input.sensitivity: high
input.transfer.high: 253
input.transfer.low: 208
input.transfer.reason: smallMomentarySpike
input.voltage: 249.00
input.voltage.maximum: 249.00
input.voltage.minimum: 247.50
output.current: 2.30
output.frequency: 50.00
output.voltage: 249.00
output.voltage.nominal: 230
ups.delay.shutdown: 0
ups.delay.start: 0
ups.firmware: UPS 06.5 / MCU 11.0 / UBL 08.2 / MBL 11.0 (ID18)
ups.id: UPS-Understage
ups.load: 25.30
ups.mfr: APC
ups.mfr.date: 11/24/2012
ups.model: Smart-UPS 2200
ups.serial: IS1248007101
ups.status: OL
ups.temperature: 18.30
ups.test.date: 04/12/2016
ups.test.result: Ok

I just need that part that says battery.charge: 100.00

into a variable

With the script how it is I get an output of

battery.charge: 100.00
battery.current: 0.00

Any help please just to get 100.00 from the battery.charge bit.

1
  • why not query specifically for battery.charge using upsc battery.charge
    – iruvar
    Apr 21, 2016 at 15:06

4 Answers 4

2

As a single line without the intermediate variable and without grep:

output="$( upsc ups@localhost | awk '/battery\.charge/ {print $2}'
1

Your question only addresses obtaining a single variable, but one approach would be to snort the entire output of upsc into the bash environment and pick out the value you need from there. To wit:

upsc_parser()
{
    local upsc_var
    local upsc_val
    while read
    do
        upsc_var="${REPLY%: *}"
        upsc_val="${REPLY#${upsc_var}: }"
        upsc_var="${upsc_var//./_}"
        upsc_val="${upsc_val//[\"\'\\]/}"
        echo "upsc_${upsc_var}=\"${upsc_val}\""
    done
}

eval $(upsc | upsc_parser)
echo $upsc_battery_charge

That gets the value you need, and doesn't require a whole lot of extra work if you decide later that you need another one.

0

Just assign Battery charge specific output to a variable

BCHRG="$( upsc ups@localhost | grep battery.charge | awk {'print $2'} )"
0

Not that elegant, but if you want you can just use awk changing your last line script:

echo $output1 | grep battery.charge | awk {'print $2'}

And if you want to keep it as variable:

output2=$(echo $output1 | grep battery.charge | awk {'print $2'})

You must log in to answer this question.

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