Created
January 30, 2018 15:08
-
-
Save stigok/62d58f710c03fefefeb788fdec3d7918 to your computer and use it in GitHub Desktop.
Script to set brightness in steps. Never to 0.
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
# Set screen brightness | |
# Usage: setbrightness more|less|<int> | |
DIR="/sys/class/backlight/intel_backlight" | |
MAX_VALUE=$( < $DIR/max_brightness ) | |
MAX_STEP=${MAX_STEP:-8} | |
STEP_AMOUNT=$(( $MAX_VALUE / $MAX_STEP )) | |
CURRENT_VALUE=$( < $DIR/brightness ) | |
CURRENT_STEP=$(( $CURRENT_VALUE / $STEP_AMOUNT )) | |
# Echo to stderr | |
function print_err() { | |
>&2 echo "$@" | |
} | |
# Set brightness by step value | |
function setstep() { | |
STEP=$1 | |
if [[ $STEP -lt 1 || $STEP -gt $MAX_STEP ]]; then | |
print_err "Invalid step $STEP. Expected number between 1 and $MAX_STEP" | |
exit 1 | |
fi | |
# Set brightness and exit with echo's exit code | |
echo $(( $STEP * $STEP_AMOUNT )) | sudo /usr/bin/tee $DIR/brightness | |
exit $? | |
} | |
if [ -z "$1" ]; then | |
echo "Current screen brightness is at step $CURRENT_STEP ($CURRENT_VALUE)" | |
exit 0 | |
fi | |
case $1 in | |
more ) setstep $(( $CURRENT_STEP + 1 )) ;; | |
less ) setstep $(( $CURRENT_STEP - 1 )) ;; | |
*) [[ $1 =~ ^[0-9]{1,2}$ ]] && setstep $1 || print_err "Invalid argument"; exit 1 ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment