You can use pmset:
-g ps / batt displays the status of the batteries and the UPS.
Saying:
pmset -g ps
will tell you if the laptop is running on AC power or when using a battery.
To translate into a condition, the following should work:
if [[ $(pmset -g ps | head -1) =~ "AC Power" ]]; then
echo "power on!"
fi
source
share