Created
September 12, 2023 23:24
-
-
Save laraconda/b758943f3b6d5b55c6621b1f0a65e476 to your computer and use it in GitHub Desktop.
Change screen brightness by percentage in linux systems
This file contains hidden or 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
#!/bin/bash | |
# | |
# Brightness Control Script | |
# | |
# This script allows you to adjust the screen brightness on your system by | |
# providing a **percentage change**. It reads the current brightness level from | |
# /sys/class/backlight/intel_backlight/brightness, calculates the new value | |
# based on the percentage change, ensures it doesn't exceed the maximum | |
# brightness from /sys/class/backlight/intel_backlight/max_brightness, and | |
# updates the brightness accordingly. | |
# | |
# Usage: brightness_control [+x or -x] | |
# | |
# Example usage: | |
# - To increase brightness by 10%: ./brightness_control +10 | |
# - To decrease brightness by 20%: ./brightness_control -20 | |
# | |
# Author: Cesar Lara (@laraconda on github) + ChatGPT 3.5 | |
# Date: September 12, 2023 | |
# | |
# Path to brightness control files | |
brightness_file="/sys/class/backlight/intel_backlight/brightness" | |
max_brightness_file="/sys/class/backlight/intel_backlight/max_brightness" | |
# Read the current brightness and maximum brightness values | |
current_brightness=$(cat "$brightness_file") | |
max_brightness=$(cat "$max_brightness_file") | |
# Function to calculate new brightness based on percentage change | |
calculate_brightness() { | |
local percentage_change=$1 | |
local new_brightness | |
new_brightness=$((current_brightness + (max_brightness * percentage_change / 100))) | |
# Ensure the new brightness value is within bounds | |
if [ "$new_brightness" -gt "$max_brightness" ]; then | |
new_brightness="$max_brightness" | |
elif [ "$new_brightness" -lt 0 ]; then | |
new_brightness=0 | |
fi | |
echo "$new_brightness" | |
} | |
# Check for input argument | |
if [ $# -eq 1 ]; then | |
percentage_change="$1" | |
# Check if the argument starts with '+' or '-' | |
if [[ "$percentage_change" == +* ]]; then | |
# Increase brightness by the given percentage | |
percentage_change="${percentage_change#+}" | |
new_brightness=$(calculate_brightness "$percentage_change") | |
elif [[ "$percentage_change" == -* ]]; then | |
# Decrease brightness by the given percentage | |
percentage_change="${percentage_change#-}" | |
new_brightness=$(calculate_brightness "-$percentage_change") | |
else | |
echo "Invalid input format. Please use +x or -x to increase or decrease brightness." | |
exit 1 | |
fi | |
# Write the new brightness value | |
echo "$new_brightness" > "$brightness_file" | |
echo "Brightness set to $new_brightness out of $max_brightness." | |
else | |
echo "Usage: $0 [+x or -x]" | |
exit 1 | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment