Skip to content

Instantly share code, notes, and snippets.

@katef
Created January 18, 2021 05:45
Show Gist options
  • Save katef/00e557c77a89948883a3df2303520dcc to your computer and use it in GitHub Desktop.
Save katef/00e557c77a89948883a3df2303520dcc to your computer and use it in GitHub Desktop.
backlight control
#!/bin/bash -e
path=/sys/class/backlight/intel_backlight
factor=10
usage() {
echo "usage: ${0##*/} [-l label] [-f path] {up|down|raise|lower} [n]" >&2
echo " ${0##*/} [-l label] [-f path] set <n>" >&2
echo " ${0##*/} [-l label] [-f path] get" >&2
exit 1
}
percent() {
echo $(( 100 * $1 / $max ))
}
absolute() {
echo $(( $1 * $max / 100 ))
}
while getopts l: c; do
case $c in
f)
path="$OPTARG"
;;
?)
usage
;;
esac
done
shift $(($OPTIND - 1))
if [ ! -d "$path" ]; then
echo "could not stat $path" >&2
exit 1
fi
max="$(< ${path}/max_brightness)"
cur="$(< ${path}/actual_brightness)"
setbl() {
new=$1
if [ $new -gt $max ]; then
new=$max
elif [ $new -le 0 ]; then
new=1
fi
echo $new > $path/brightness
}
case "$1" in
up|down|raise|lower)
if [ $# -gt 2 ]; then
usage
exit
elif [ $# -eq 2 ]; then
factor="$2"
fi
if [ ! \( "$factor" -ge 0 -a "$factor" -le 100 \) ]; then
echo "out of range" >&2
exit 1
fi
new=`absolute $factor`
case "$1" in
up) setbl $(( $cur + $new )) ;;
down) setbl $(( $cur - $new )) ;;
raise) setbl $(( $cur * $factor )) ;;
lower) setbl $(( $cur / $factor )) ;;
esac
;;
set)
if [ $# -ne 2 ]; then
usage
exit
fi
newp="$2"
if [ ! \( "$newp" -ge 0 -a "$newp" -le 100 \) ]; then
echo "out of range" >&2
exit 1
fi
setbl $(( $max * $newp / 100 ))
;;
get)
percent $cur
;;
*)
usage
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment