Skip to content

Instantly share code, notes, and snippets.

@ldante86
Created November 16, 2016 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ldante86/f4d8775cb51ecda24b1115c7bc6947a2 to your computer and use it in GitHub Desktop.
Save ldante86/f4d8775cb51ecda24b1115c7bc6947a2 to your computer and use it in GitHub Desktop.
bash version of acpi
#!/bin/bash -
#
# SCRIPT: aacpi
# AUTHOR: Luciano D. Cecere
# DATE: 2014
#
########################################################################
#
# acpi - show laptop battery information
# Copyright (C) 2014 Luciano D. Cecere <ldante86@aol.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#########################################################################
export PATH=/bin:/usr/bin
unalias -a
set -e
PROGRAM="${0##*/}"
case $1 in
-h|--help)
echo "\
Usage: ${PROGRAM} [OPTION]
-a show adapter info
-h show this help and exit
-i show battery details
-V show all battery details
"
exit 0
;;
esac
PROC=/proc/acpi
if [ ! -d ${PROC} ]; then
echo Battery information is not available.
echo ${PROC} cannot be found.
exit 1
fi
AC_ADAPTER=${PROC}/ac_adapter/ACAD/state
BATTERY_STATE=${PROC}/battery/BAT*/state
BATTERY_INFO=${PROC}/battery/BAT*/info
AC_STATE=$(cat $AC_ADAPTER | cut -d":" -f 2 | sed 's/^ *//')
REMAING_CAPACITY=$(cat $BATTERY_STATE | grep -i remaining | tr -d 'a-zA-Z: ')
PRESENT_RATE_OF_DISCHARGE=$(cat $BATTERY_STATE | grep -i rate | tr -d 'a-zA-Z: ')
LAST_FULL_CAPACITY=$(cat $BATTERY_INFO | grep -i last | tr -d 'a-zA-Z: ')
SECONDS_IN_DAY=86400
SECONDS_IN_HOUR=3600
MINUTES_IN_HOUR=60
SECONDS_IN_MINUTE=60
((CURRENT_BATTERY_PERCENTAGE=REMAING_CAPACITY*100/LAST_FULL_CAPACITY))
_show_battery_info()
{
case $AC_STATE in
on-line)
if ((CURRENT_BATTERY_PERCENTAGE==100)); then
echo Battery 0: Full, ${CURRENT_BATTERY_PERCENTAGE}%
else
SECONDS=$((3600*(LAST_FULL_CAPACITY-REMAING_CAPACITY)/PRESENT_RATE_OF_DISCHARGE))
printf "%s %02d:%02d:%02d %s\n" \
"Battery 0: Charging, ${CURRENT_BATTERY_PERCENTAGE}%," \
"$((SECONDS/SECONDS_IN_HOUR))" \
"$(((SECONDS/MINUTES_IN_HOUR)%MINUTES_IN_HOUR))" \
"$((SECONDS%SECONDS_IN_MINUTE))" \
"until charged"
fi
;;
off-line)
SECONDS=$((3600*REMAING_CAPACITY/PRESENT_RATE_OF_DISCHARGE))
printf "%s %02d:%02d:%02d %s\n" \
"Battery 0: Discharging, ${CURRENT_BATTERY_PERCENTAGE}%," \
"$((SECONDS/SECONDS_IN_HOUR))" \
"$(((SECONDS/MINUTES_IN_HOUR)%MINUTES_IN_HOUR))" \
"$((SECONDS%SECONDS_IN_MINUTE))" \
"remaining"
;;
esac
}
_show_battery_details()
{
CAPACITY=$(cat $BATTERY_INFO | grep -i 'design capacity:' | tr -d 'a-zA-Z: ')
LF_CAPACITY=$(cat $BATTERY_INFO | grep -i last )
CAPACITY=$(bc <<< "scale=2 ; $LAST_FULL_CAPACITY/$CAPACITY" | tr -d .)
DESIGN_CAPACITY=$(cat $BATTERY_INFO | grep -i 'design capacity:')
}
case $1 in
-a|--ac-adapter)
echo Adapter 0: $AC_STATE
;;
-i|--details)
_show_battery_info
_show_battery_details
echo Battery 0: ${DESIGN_CAPACITY}, ${LF_CAPACITY} = ${CAPACITY}%
;;
-V|--everything)
_show_battery_info
cat $AC_ADAPTER
cat $BATTERY_STATE
cat $BATTERY_INFO
;;
*)
_show_battery_info
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment