Skip to content

Instantly share code, notes, and snippets.

@diego09310
Last active February 20, 2021 02:09
Show Gist options
  • Save diego09310/b907cda1d60534b69aee64c07a7cfcac to your computer and use it in GitHub Desktop.
Save diego09310/b907cda1d60534b69aee64c07a7cfcac to your computer and use it in GitHub Desktop.
Script to toggle the screen backlight on and off in Linux
#!/bin/bash
#
# Toggles on and off the screen backlight
#
# It saves the current brightness value, if there is no previous value,
# defaults to 1/10th of max brightness
#
# More information in https://diego09310.github.io/linux/2020/02/19/turn-off-dell-xps-screen-linux.html
#
brightness_path=/sys/class/backlight/intel_backlight/brightness
current_brightness=$(cat $brightness_path)
max_brightness_path=/sys/class/backlight/intel_backlight/max_brightness
max_brightness=$(cat $max_brightness_path)
prev_on_value_path=$(dirname $(realpath $0))/prev_brightness
log_path=$(dirname $(realpath $0))/log.txt
debug=false
log() {
if [ "$debug" = true ]; then
echo $@ >> $log_path
fi
}
set_brightness() {
log "Setting brightness to $1"
echo $1 | tee $brightness_path
}
set_previous_brightness() {
echo $1 | tee $prev_on_value_path
}
if [ "$current_brightness" -eq "0" ]; then
if [ ! -f "$prev_on_value_path" ]; then
set_previous_brightness $(( max_brightness / 10 ))
fi
set_brightness $(cat $prev_on_value_path)
else
set_previous_brightness $current_brightness
set_brightness 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment