Created
December 1, 2022 14:14
-
-
Save TheFlash2k/64b778d2709d613b46f1ef9c94f7ed64 to your computer and use it in GitHub Desktop.
Increase or decrease the brightness of your monitor using arguments to this script
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 | |
############################# | |
#### Author: @TheFlash2k #### | |
############################# | |
########################################################################### | |
###### Change this to the amount of brightness you want to inc/dec ######## | |
base=0.1 | |
########################################################################### | |
############################################### | |
############## Requirements: ################## | |
##### xrandr (For brightness) ################# | |
##### bc (For basic math) ################# | |
############################################### | |
RED="\e[31m" | |
GREEN="\e[32m" | |
ENDCOLOR="\e[0m" | |
ERROR="[${RED}-${ENDCOLOR}] ${RED}Error${ENDCOLOR}:" | |
usage() { | |
echo -e $1; echo; | |
echo "Usage: $0 [ [UP|DOWN] | NUMBER ]" | |
echo " UP -- Increases the brightness by 0.1" | |
echo " DOWN -- Decreases the brightness by 0.1" | |
echo " -h|--help -- Prints the help Menu" | |
echo " <NUMBER> -- Changes the brightness to an exact number (Between 0 to 1)" | |
exit $2 | |
} | |
# Checks if the command exist: | |
check_command() { | |
($1 -h) &>/dev/null | |
if [ $? == 127 ] && [ $? != 0 ] && [ $? != 1 ]; then | |
echo -e "[${RED}-${ENDCOLOR}] ${RED}$1${ENDCOLOR} command not found." | |
exit 1 | |
fi | |
} | |
check() { | |
re='0(\.\d+)?|1\.0' | |
np_re='(\.\d+)?|1\.0' | |
if ! [[ $arg =~ $re ]] ; then | |
if ! [[ $arg =~ $np_re ]]; then | |
usage "[${RED}-${ENDCOLOR}] ${RED}Error${ENDCOLOR}: Invalid value specified! Please specify a value between 0 and 1" 1 | |
fi | |
fi | |
check_new $arg | |
} | |
check_up() { | |
res=$(echo "$1>=1"| bc) | |
if [[ $res != 0 ]]; then | |
echo -e "${ERROR} Cannot increase value to more than 1. (Current: $curr_brightness)" | |
exit 1 | |
fi | |
} | |
check_down() { | |
## Checking if the value is <= 0: | |
res=$(echo "$1<=0"| bc) | |
if [[ $res != 0 ]]; then | |
echo -e "${ERROR} Cannot decrease value to less than or equal to 0. (Current: $curr_brightness)" | |
exit 1 | |
fi | |
} | |
check_new() { | |
# This function checks whether the brightness has exceeded the 1 or is equal to or less than 0 | |
if [[ $2 == "up" ]]; then | |
check_up $1 | |
elif [[ $2 == "down" ]]; then | |
check_down $1 | |
else | |
check_up $1 | |
check_down $1 | |
fi | |
} | |
check_command "xrandr" | |
check_command "bc" | |
monitor=$(xrandr --listactivemonitors | cut -d $'\n' -f 2 | cut -d ' ' -f 6) | |
curr_brightness=$(xrandr --verbose | grep -i brightness | cut -d ' ' -f 2) | |
# echo "Monitor: $monitor" | |
# echo "Brightness: $curr_brightness" | |
# echo "New Brightness: $new_brightness" | |
if [[ $# != 1 ]]; then | |
usage "${ERROR} No/Invalid Arguments have been provided" 1 | |
fi | |
# Checking the passed arguments: | |
## Converting the passed argument to lowercase: | |
arg=$(echo $1 | tr '[:upper:]' '[:lower:]') | |
if [[ "$arg" == "up" ]]; then | |
new_brightness=$(echo "$curr_brightness $base" | awk '{print $1+$2}') | |
check_new $new_brightness "up" | |
echo -e "[${GREEN}+${ENDCOLOR}] Increasing Brightness from $curr_brightness to $new_brightness" | |
xrandr --output $monitor --brightness $new_brightness | |
elif [[ "$arg" == "down" ]]; then | |
new_brightness=$(echo "$curr_brightness $base" | awk '{print $1-$2}') | |
check_new $new_brightness "down" | |
echo -e "[${GREEN}+${ENDCOLOR}] Decreasing Brightness from $curr_brightness to $new_brightness" | |
xrandr --output $monitor --brightness $new_brightness | |
elif [[ "$arg" == "-h" ]]; then | |
usage | |
elif [[ "$arg" == "--help" ]]; then | |
usage | |
else | |
check $1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment