![用于获取两块电池的电量和剩余时间详细信息的命令](https://linux22.com/image/131233/%E7%94%A8%E4%BA%8E%E8%8E%B7%E5%8F%96%E4%B8%A4%E5%9D%97%E7%94%B5%E6%B1%A0%E7%9A%84%E7%94%B5%E9%87%8F%E5%92%8C%E5%89%A9%E4%BD%99%E6%97%B6%E9%97%B4%E8%AF%A6%E7%BB%86%E4%BF%A1%E6%81%AF%E7%9A%84%E5%91%BD%E4%BB%A4.png)
我有一台带有两块电池的笔记本电脑。我想获得两种电池的综合详细信息。特别是,我想要两个电池电量耗尽之前的剩余时间以及两个电池中剩余电量的百分比。有命令可以做到这一点吗?
当我跑步时:
acpi -b
我得到以下输出:
Battery 0: Full, 100%
Battery 1: Discharging, 80%, 05:10:03 remaining
所以我想要一个命令,而不是给我类似的东西:
All batteries: Discharging 90%, 10:10:06 remaining
答案1
这是我的脚本。这取决于acpi
和acpitool
它:
输出设备中所有电池的平均百分比
所有电池充满电需要多长时间(如果设备已插入电源),或者电池完全耗尽需要多长时间(如果未插入电源),
表示设备是否正在充电。
最终输出的格式是All batteries: Discharging 90%, 10:10:06 remaining
(不同的数字,放电可以是充电)。
#!/bin/bash
get_time_until_charged() {
# parses acpitool's battery info for the remaining charge of all batteries and sums them up
sum_remaining_charge=$(acpitool -B | grep -E 'Remaining capacity' | awk '{print $4}' | grep -Eo "[0-9]+" | paste -sd+ | bc);
# finds the rate at which the batteries being drained at
present_rate=$(acpitool -B | grep -E 'Present rate' | awk '{print $4}' | grep -Eo "[0-9]+" | paste -sd+ | bc);
# divides current charge by the rate at which it's falling, then converts it into seconds for `date`
seconds=$(bc <<< "scale = 10; ($sum_remaining_charge / $present_rate) * 3600");
# prettifies the seconds into h:mm:ss format
pretty_time=$(date -u -d @${seconds} +%T);
echo $pretty_time;
}
get_battery_combined_percent() {
# get charge of all batteries, combine them
total_charge=$(expr $(acpi -b | awk '{print $4}' | grep -Eo "[0-9]+" | paste -sd+ | bc));
# get amount of batteries in the device
battery_number=$(acpi -b | wc -l);
percent=$(expr $total_charge / $battery_number);
echo $percent;
}
get_battery_charging_status() {
if $(acpi -b | grep --quiet Discharging)
then
echo "Discharging";
else # acpi can give Unknown or Charging if charging, https://unix.stackexchange.com/questions/203741/lenovo-t440s-battery-status-unknown-but-charging
echo "Charging";
fi
}
echo "All batteries: $(get_battery_charging_status) $(get_battery_combined_percent)%, $(get_time_until_charged ) remaining";