Skip to content

Instantly share code, notes, and snippets.

@walshc
Created September 9, 2017 14:39
Show Gist options
  • Save walshc/470af4076b3001a34bfd1ab3557824af to your computer and use it in GitHub Desktop.
Save walshc/470af4076b3001a34bfd1ab3557824af to your computer and use it in GitHub Desktop.
Script to set/increase/decrease brightness in Ubuntu 17.04
#!/bin/bash
# Script to set/increase/decrease brightness in Ubuntu
MAX_BRIGHTNESS=$(cat /sys/class/backlight/intel_backlight/max_brightness);
BRIGHTNESS=$(cat /sys/class/backlight/intel_backlight/actual_brightness);
PCT=$(echo $BRIGHTNESS $MAX_BRIGHTNESS | awk '{printf "%4.0f\n",($1/$2)*100}')
if [ "$(whoami)" != "root" ] ; then
printf "Script must be run as root.\n" && exit;
fi
if [[ $1 == "-s" ]] || [[ $1 == "--set" ]]; then
if [[ $1 -lt 0 ]] || [[ $1 -gt 100 ]]; then
printf "Brightness must be between 0 and 100.\n" && exit;
fi
BRIGHTNESS=$(echo $2 $MAX_BRIGHTNESS | awk '{printf "%4.0f\n",($1/100)*$2}')
elif [[ $1 == "-i" ]] || [[ $1 == "--inc" ]]; then
BRIGHTNESS=$(echo $PCT $2 $MAX_BRIGHTNESS | \
awk '{printf "%4.0f\n",(($1+$2)/100)*$3}')
elif [[ $1 == "-d" ]] || [[ $1 == "--dec" ]]; then
BRIGHTNESS=$(echo $PCT $2 $MAX_BRIGHTNESS | \
awk '{printf "%4.0f\n",(($1-$2)/100)*$3}')
else
printf "Usage: brightness -s 50 # Set brightness at 50%%\n"
printf " brightness -i 10 # Increase brightness by 10%%\n"
printf " brightness -d 10 # Decrease brightness by 10%%\n"
fi
# Contstrain brightness to be in the range:
if [[ $BRIGHTNESS -gt $MAX_BRIGHTNESS ]]; then
BRIGHTNESS=$MAX_BRIGHTNESS
elif [[ $BRIGHTNESS -lt 0 ]]; then
BRIGHTNESS=0
fi
echo $BRIGHTNESS > /sys/class/backlight/intel_backlight/brightness
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment