项目作者: novakcgx

项目描述 :
:battery: Bash script to print your Linux computer's battery level.
高级语言: Shell
项目地址: git://github.com/novakcgx/battery.git
创建时间: 2021-07-14T06:32:32Z
项目社区:https://github.com/novakcgx/battery

开源协议:Creative Commons Zero v1.0 Universal

下载


battery

A Bash program to print your Linux machine’s battery level.

  1. Download
  2. Use
  3. Example output
  4. Exit codes
  5. How it works
  6. Why I made this
  7. License

Download

  1. curl -O https://cszach.github.io/battery/battery
  2. chmod +x battery
  3. mv battery ~/.local/bin # Optional

Use

Get help

  1. battery --help

Print the battery level

  1. battery

Print the battery level of BAT1

  1. battery BAT1

List batteries and power supplies

  1. battery --list

Print the battery level and colorize

  1. battery --color=always

Print the battery level of BAT1 and colorize only if it is charging

  1. battery --color=charging BAT1

Example output

  1. 75%

Exit codes

  1. echo $?
  • 0: Successful.
  • 1: Invalid option.
  • 2: Battery could not be found.
  • 3: Battery’s level could not be accessed.

How it works

  • Power supplies are found in /sys/class/power_supply.
  • The battery level is found in /sys/class/power_supply/BATTERY/capacity where
    BATTERY is a power source detected by your Linux system.
  • The charging state is found in /sys/class/power_supply/BATTERY/status. The
    content of this file is either Charging, Discharging, Full, or
    Unknown.

Why I made this

I didn’t want to use a graphical desktop on my Parabola GNU/Linux-libre
laptop so I needed a way to know the battery level in the command line. I wanted
the output to be colorized, too (e.g. green for good battery level, red for weak
battery), so it was more than just a one-liner. Ultimately, I figured out I
could share this little Bash script with the Internet, so I decided to support
user-given power source and push this onto GitHub.

You can easily integrate this program with your PS1 command prompt, like this:

  1. # ~/.bashrc
  2. PS1="$(battery) $"

License

This work is licensed under the Creative Commons Zero v1.0 Universal
and is therefore a dedication to the public domain. This means you can copy,
modify, and redistribute the program without asking for permission.