ターミナルにBTマウス/BTキーボードのバッテリ残量警告を表示する

$ vim ~/battery_alert.sh
---
#!/bin/bash
BATTERY_ALERT_PERCENT=25
MAX_TRY=10
num=0
while [ $num -lt 10 ]
do
    product_name=$(ioreg -arl -c AppleDeviceManagementHIDEventService -k BatteryPercent -d 1 > /tmp/hid_info.plist \
            && /usr/libexec/PlistBuddy -c "Print $num:Product" /tmp/hid_info.plist 2>/dev/null)
    battery_percent=$(ioreg -arl -c AppleDeviceManagementHIDEventService -k BatteryPercent -d 1 > /tmp/hid_info.plist \
            && /usr/libexec/PlistBuddy -c "Print $num:BatteryPercent" /tmp/hid_info.plist 2>/dev/null)
    if [ ! $? -eq 0 ]; then
        break
    fi

    if [ $battery_percent -lt $BATTERY_ALERT_PERCENT ]; then
        echo "$product_name is almost dead: $battery_percent %" | cowsay -f  hellokitty.cow
    fi

    num=$(expr $num + 1)
done
---

$ echo "~/battery_alert.sh" >> ~/.bash_profile

表示例

 ____________________________________
< Magic Mouse 2 is almost dead: 22 % >
 ------------------------------------
  \
   \
      /\_)o<
     |      \
     | O . O|
      \_____/
develop43-no-iMac:~ develop43$