用于获取两块电池的电量和剩余时间详细信息的命令

用于获取两块电池的电量和剩余时间详细信息的命令

我有一台带有两块电池的笔记本电脑。我想获得两种电池的综合详细信息。特别是,我想要两个电池电量耗尽之前的剩余时间以及两个电池中剩余电量的百分比。有命令可以做到这一点吗?

当我跑步时:

acpi -b

我得到以下输出:

Battery 0: Full, 100%
Battery 1: Discharging, 80%, 05:10:03 remaining

所以我想要一个命令,而不是给我类似的东西:

All batteries: Discharging 90%, 10:10:06 remaining

答案1

这是我的脚本。这取决于acpiacpitool

它:

  1. 输出设备中所有电池的平均百分比

  2. 所有电池充满电需要多长时间(如果设备已插入电源),或者电池完全耗尽需要多长时间(如果未插入电源),

  3. 表示设备是否正在充电。

最终输出的格式是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";

相关内容