#!/bin/bash BAT_PATH=/sys/class/power_supply BAT=BAT0 MODE=suspend-then-hibernate # 0: charging # 1: discharging/unknown function _state() { f=$BAT_PATH/$BAT/status s=1 if [ -e $f ]; then s=$(grep -c Discharging $f) fi echo $s } function _suspend() { state=$(_state) force=${1:-0} echo "INFO: state: $state, force: $force" if [ $state -eq 1 -o $force -eq 1 ]; then systemctl $MODE else echo "ERROR: state: $state, force: $force" fi } case $1 in suspend) _suspend 0 ;; force) _suspend 1 ;; *) echo "ERROR: invalid command: \"$1\"" echo "possible options: [suspend|force]" ;; esac