Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 179 lines (141 sloc) 4.31 KB
#!/usr/bin/env bash
###
### battery: Display properties specific to the battery of a Mac. Inspired by https://gist.github.com/earlonrails/9374070
###
if [[ "$(uname)" != "Darwin" ]]; then
printf 'This can only be ran on a macOS machine.\n'
exit 1
fi
# charging: Determine if the battery potential is under capacity, and in a power-gain state.
charging () {
local IS_CHARGING=$(ioreg -r -n AppleSmartBattery | grep IsCharging | cut -c22-)
if [[ "$IS_CHARGING" == "Yes" ]]; then
return 0
else
return 1
fi
}
# connected: Determine if the battery is connected to an external power source.
connected () {
local IS_CONNECTED=$(ioreg -r -n AppleSmartBattery | grep ExternalConnected | cut -c29-)
if [[ "$IS_CONNECTED" == "Yes" ]]; then
return 0
else
return 1
fi
}
# timeleft: Determine the amount of time (in minutes) left until the battery is either charged or drained.
timeleft () {
MINS_LEFT=$(ioreg -r -n AppleSmartBattery | grep TimeRemaining | cut -c25-)
return $MINS_LEFT
}
# usage: Display usage information
usage () {
local USAGE=
read -r -d '' USAGE <<-'EOF'
Usage: battery [OPTIONS]
Options:
--help, -h Show usage details
--amperage, -A Show battery amperage (in milliamps).
--is-charging, -C Whether battery potential is under capacity and in a power-gain (charging) state.
--is-fully-charged, -F Whether battery is at capacity and not in a power-gain (charging) state.
--cycle-count, -K Show number of completed charge/discharge cycles.
--minutes-until-charged, -M Show minutes remaining until battery is fully charged.
--minutes-until-discharged, -O Show minutes remaining until battery is fully discharged.
--is-power-connected, -P Whether battery is connected to a power source.
--serial-number, -S Show battery serial number.
--temperature, -T Show battery temperature in Fahrenheit, Celsius, and Kelvin.
--voltage, -V Show battery voltage (in millivolts)
--manufacturer, -W Show battery manufacturer.
EOF
echo "$USAGE"
}
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
usage
break
;;
-A|--amperage)
shift
AMPERAGE=$(ioreg -r -n AppleSmartBattery | grep -w '\s"Amperage"' | cut -c20-)
printf '%smA\n' "$AMPERAGE"
;;
-C|--is-charging)
shift
if charging; then
printf 'true\n'
else
printf 'false\n'
fi
;;
-F|--is-fully-charged)
shift
IS_FULLY_CHARGED=$(ioreg -r -n AppleSmartBattery | grep FullyCharged | cut -c24-)
if [[ "$IS_FULLY_CHARGED" == "Yes" ]]; then
printf 'true\n'
else
printf 'false\n'
fi
;;
-K|--cycle-count)
shift
CYCLE_COUNT=$(ioreg -r -n AppleSmartBattery | grep '"CycleCount"' | cut -c22-)
printf '%s\n' "$CYCLE_COUNT"
;;
-M|--minutes-until-charged)
shift
MINS_LEFT=$(ioreg -r -n AppleSmartBattery | grep TimeRemaining | cut -c25-)
if ! connected; then
printf 'The battery is not connected to a power source.\n'
break
fi
printf '%s\n' "$MINS_LEFT"
;;
-O|--minutes-until-discharged)
shift
MINS_LEFT=$(ioreg -r -n AppleSmartBattery | grep TimeRemaining | cut -c25-)
if connected; then
printf 'The battery is connected to a power source.\n'
break
fi
printf '%s\n' "$MINS_LEFT"
;;
-P|--is-power-connected)
shift
if connected; then
printf 'true\n'
else
printf 'false\n'
fi
;;
-S|--serial-number)
shift
SERIAL=$(ioreg -r -n AppleSmartBattery | grep BatterySerialNumber | cut -c31- | tr -d \")
printf '%s\n' "$SERIAL"
;;
-T|--temperature)
shift
TEMP=$(ioreg -r -n AppleSmartBattery | grep Temperature | cut -c23-)
TEMP_C=$(echo "scale = 2; $TEMP / 100.0" | bc)
TEMP_F=$(echo "scale = 2; ($TEMP_C * (9 / 5)) + 32.0" | bc)
TEMP_K=$(echo "scale = 2; $TEMP_C + 273.15" | bc)
printf '%s °C\n%s °F\n%s °K\n' "$TEMP_C" "$TEMP_F" "$TEMP_K"
;;
-V|--voltage)
shift
VOLTAGE=$(ioreg -r -n AppleSmartBattery | grep '\s"Voltage"' | cut -c19-)
printf '%smV\n' "$VOLTAGE"
;;
-W|--manufacturer)
shift
MANUFACTURER=$(ioreg -r -n AppleSmartBattery | grep '"Manufacturer"' | cut -c24- | tr -d \")
printf '%s\n' "$MANUFACTURER"
;;
*)
printf 'Invalid option %s\n\n' "$1"
usage
break
;;
esac
done