When I’m running a script that might be somewhat time intensive I like to check the battery of the MacBooks first. Otherwise I might end up hosing some machines that die out in the middle of a script. To do so I’ll use ioreg to grab the maximum load that a battery can sustain, stored in MaxCapacity:
capacity=`ioreg -l | grep MaxCapacity | cut -c 35-39`
Then I’ll grab the current load on the battery, stored in CurrentCapacity:
current=`ioreg -l | grep CurrentCapacity | cut -c 39-43`
Finally I’ll grab a percentage:
echo “scale=2; $current*100/$capacity” | bc
If the percentage is above a certain threshold then I’ll run the script, if not I’ll exit the script. Usually I’ll return a different code on success or failure for the sanity check than I would for success or failure of the actual payload, if only to ease deployment troubleshooting.