Created
March 13, 2017 09:26
-
-
Save toke/938f6cbce11479e0b27e3549bc6ec7a3 to your computer and use it in GitHub Desktop.
AC Power script for shell scripting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
function usage() { | |
cat << EOF | |
$0 - Returns exit code 1 when on battery power | |
Backup is only run when on AC power (see --no-ac-check option). | |
Example usage: "$0 && backup.sh" in cron job | |
USAGE: | |
-h|--help help | |
-q quiet, no message | |
--no-ac-check disables AC power check (i.e. does nothing) | |
ENV: | |
NO_AC_CHECK=1 same as --no-ac-check | |
EOF | |
} | |
### AC Power check, can be overrwritten by the NO_AC_CHECK variable | |
function ac_check () { | |
if [[ "${NO_AC_CHECK}" -ne 1 && $(cat /sys/class/power_supply/AC/online) -eq 0 ]] ; then | |
test "${QUIET}" != "1" && echo "W: Skipped due to battery check" | |
exit 1 | |
else | |
exit 0 | |
fi | |
} | |
### Command line processing | |
for i in "$@" | |
do | |
case $i in | |
-h|--help) | |
shift | |
usage | |
exit 2 | |
;; | |
--no-ac-check) | |
NO_AC_CHECK=1 | |
shift | |
;; | |
-q) | |
QUIET=1 | |
shift | |
;; | |
*) | |
echo "Unknown Option ${i}" | |
echo "Use $0 -h for usage" | |
exit 3 | |
# unknown option | |
;; | |
esac | |
done | |
### Run default commands | |
ac_check |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment