Ubuntu – Custom command – one less than current brightness level

brightnesscommand line

I tried using xbacklight, but it wasnt working so I made some custom global commands to control the brightness. For example:

"Bright" :

sudo bash -c "echo 20 > /sys/class/backlight/acpi_video0/brightness"

"Mid" :

sudo bash -c "echo 10 > /sys/class/backlight/acpi_video0/brightness"

"Dark" :

sudo bash -c "echo 0 > /sys/class/backlight/acpi_video0/brightness"

I want to make two more commands, one that will increase the brightness by one and one that will decrease the brightness by one.

Is there some code I can use in place of 'echo "NUMBER"' that will decrease / increase the current value by one.

I know that the current brightness level is the document: /sys/class/backlight/acpi_video0/brightness which currently reads "20" and changes when using the brightness commands.

I have no idea what this command would be like but something like

sudo bash -c "echo [ONE LESS THAN] /sys/class/backlight/acpi_video0/brightness > /sys/class/backlight/acpi_video0/brightness" 

I dont know if this is possible, but thanks for any help.

Update

What is the correct usage?

When I run "sudo ./brightness.sh +1" it outputs :

Usage:
sudo brightness.sh [ + | - | INTEGER ]
./brightness.sh: 10: [: +: unexpected operator
./brightness.sh: 23: [: +: unexpected operator
./brightness.sh: 38: ./brightness.sh: [[: not found
<<< ERROR: wrong parameter 
./brightness.sh: 44: ./brightness.sh: printUsage: not found

and when I run "sudo bash brightness.sh +1" it outputs :

Usage:
sudo brightness.sh [ + | - | INTEGER ]
brightness.sh: line 27: [: -1: unary operator expected
brightness.sh: line 34: : No such file or directory

Best Answer

Why not make one command that will increase by one or decrease by one based on the options you pass to it ? That's the basic idea behind the script you will see bellow. We store current value in CURRENT variable, then use bash's arithmetic expansion $(( numberA + numberB )) to increase or decrease by one. As for decision making, we call the script with single command line argument + to increase , or - for decrease.

Per muru's suggestion in the comments , I also made a small edit to the code, which now allows you to call brightness.sh with an integer parameter less than or equals to the max brightness

#!/bin/bash

###########
# Variables
###########
FILE="/sys/class/backlight/acpi_video0/brightness"
MAX="$(cat /sys/class/backlight/acpi_video0/max_brightness)"
MIN=0
CURRENT="$( cat  $FILE )"

###########
# functions
###########

function printUsage
{
  echo "Usage:"
  echo "sudo brightness.sh [ + | - | INTEGER ]"
}

######
# Main
######


if [ $# -eq 0  ];
then
   printUsage
   exit 0
fi


if [ "$1" == "+" ];
then
   NEW=$(( $CURRENT+1  ))
   # are we trying to go past maximum value ? If yes, quit
   if [ $NEW -ge $MAX  ]
   then 
       echo ">>> ERROR: Current value already the maximum; "
       echo ">>> Exiting"
       exit 1
   fi
   # if not, then proceed
   echo "$NEW" > "$FILE"

elif [ "$1" == "-"  ];
then
   NEW=$(( $CURRENT-1  ))
   # Is NEW value going past 0 ? If yes, quit
   if [ $NEW -le $MIN ]; 
   then
       echo ">>> Error: already at minimum. "
       echo ">>> Exiting."
       exit 1
    fi
    # If not, then continue
    echo "$NEW" >  "$FILE"

# Note: new test w/ regex; for portability
# probably better idea to use awk
elif [[ "$1" =~ ^[0-9]+$ ]] && [ $1 -le $MAX   ];
then
    echo "$1" > "$FILE"

else
    echo "<<< ERROR: wrong parameter "
    printUsage
    exit 1
fi