Skip to content

Instantly share code, notes, and snippets.

@qoopooh
Created August 22, 2022 06:28
Show Gist options
  • Save qoopooh/0e06d5ba30d3754e2c768c6edf627d3e to your computer and use it in GitHub Desktop.
Save qoopooh/0e06d5ba30d3754e2c768c6edf627d3e to your computer and use it in GitHub Desktop.
Check MacBook battery with command line
#! /bin/sh
#
# Check battery on macbook air 2017
#
UNAME=$(uname)
if [ "$UNAME" = "Linux" ]; then
# find a battery
CMD="upower -e"
echo $CMD
OUT=$($CMD)
BAT=$(echo "$OUT" | grep 'BAT')
CMD="upower -i $BAT"
echo -e "$CMD\n"
OUT=$($CMD)
PERCENT=$(echo "$OUT" | grep -Eo 'percentage:.+')
echo $PERCENT
TIME=$(echo "$OUT" | grep -Eo 'time to empty:.+')
echo $TIME
CYCLE=$(echo "$OUT" | grep -Eo 'charge-cycles:.+')
echo $CYCLE
else
echo "pmset -g batt\n"
pmset -g batt | grep -Eo '\d{1,3}%.*'
printf "cycle: "
system_profiler SPPowerDataType | grep "Cycle Count" | awk '{print $3}'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment