Skip to content

Instantly share code, notes, and snippets.

@jondthompson
Forked from earlonrails/battery_temperature.sh
Last active July 13, 2017 15:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jondthompson/11c4f27d4f3c88946659 to your computer and use it in GitHub Desktop.
Save jondthompson/11c4f27d4f3c88946659 to your computer and use it in GitHub Desktop.
#!/bin/bash
# battery_temperature for mac osx 10.9.2
if [ `uname` == "Darwin" ]; then
if [ "$1" == "-h" ]; then
echo "Usage: $(basename $0 ".sh") [option]"
echo " -l also show Model Name and Processor Name"
echo " -h display this help message"
exit
fi
# -l display Model Name and Processor Name
if [ "$1" == "-l" ]; then
system_profiler SPHardwareDataType | awk '/Model Name/ || /Processor Name/' | sed 's/[^:]*: //' | awk '{ printf $0 " " }'
echo
fi
# sensor data
temperature=`ioreg -r -n AppleSmartBattery | grep Temperature | cut -c23-`
temperature_celsius=`echo "scale = 2; $temperature / 100.0" | bc`
echo "AppleSmartBattery Temperature: "
echo $temperature_celsius °C
temperature_fahrenheit=`echo "scale = 2; ($temperature_celsius * (9 / 5)) + 32.0" | bc`
echo $temperature_fahrenheit °F
# error message if unsupported machine
else
echo -e "\nThis appears not to be an OS X machine\n"
fi
@jondthompson
Copy link
Author

Fixed a bug mentioned by bkovitz in the comments of the original gist.

@bradleeedmondson
Copy link

@jondthompson Do you know if this sensor is physically located in the battery? And if so, if there is a way to find cpu temp using ioreg?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment