Skip to content

Instantly share code, notes, and snippets.

@techiev2
Last active December 15, 2018 17:43
Show Gist options
  • Save techiev2/0faf34191b4505efdecf04097b33a661 to your computer and use it in GitHub Desktop.
Save techiev2/0faf34191b4505efdecf04097b33a661 to your computer and use it in GitHub Desktop.
Shell script to set the keyboard background brightness on Ubuntu installs.
#! /bin/bash
# Utility script to set keyboard backlight level from command line.
# The kernel that ships with stock Ubuntu lacks the hardware support
# for certain devices like the keyboard backlight.
# Ref: [1], [2].
# This script uses the tweak suggested here([3]) to set the background
# light levels.
# Caveat emptor: The script uses the max_brightness file in the device
# data file to determine the maximum steop allowed for the device.
# In case the file isn't available, the script falls back to 0 and 100
# for current and maximum steo values.
# My Asus TP301UJ has three levels so my min value is 0 (no light)
# and max value is 3 (full). Please update the levels according to your
# last known configuration/step count.
# [1] https://askubuntu.com/a/1048524
# [2] https://bugs.launchpad.net/ubuntu/+source/upower/+bug/1791372
# [3] https://askubuntu.com/a/1088833
# Alternatively, as suggested in one of the forum responses, you could
# upgrade the kernel to one that supports. Personally, I prefer waiting
# out until a stable kernel with a proper fix appears upstream.
# Changeset1: Uses sudo -i to allow for interactive sudo + captures echo response to a variable instead of terminal echo
# TODO: Allow for non-terminal usage of the script.
device="/sys/class/leds/asus::kbd_backlight"
brightnessFile="$device/brightness"
maxFile="$device/max_brightness"
if [[ ! -f "$maxFile" ]]; then
max=100
else
max=$(cat $maxFile)
fi
if [[ ! -f "$brightnessFile" ]]; then
curr=0
else
curr=$(cat "$brightnessFile")
fi
min=0
txt="Select level for keyboard brightness ($min - $max)"
val=$(zenity --scale --text="$txt" --min-value=$min --max-value=$max --value=$curr --step=1)
res=$(echo $val | sudo -i tee less "$brightnessFile" 2>&1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment