1

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
Carter Roeser
  • 513
  • 2
  • 6
  • 8
  • Related (or an alternative): http://askubuntu.com/a/672670/72216 – Jacob Vlijm Dec 31 '15 at 21:41
  • It's not +1 or -1, but either +, or -, or some number without any sign. – muru Jan 05 '16 at 07:17
  • Hi Carter ! Like muru already told you, the correct usage is sudo ./brightness.sh + or sudo ./brightness.sh + or sudo ./brightness.sh 10. If you wish , you can change my code to be less confusing or I can do that in my answer . – Sergiy Kolodyazhnyy Jan 05 '16 at 07:26

1 Answers1

3

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
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • You can probably use a parameter other than + or - to mean the absolute value of brightness to be set: else echo "$1" > "$FILE"; fi, combining all three use cases. – muru Dec 31 '15 at 20:28
  • @muru added the suggested case, now can use integer parameters – Sergiy Kolodyazhnyy Dec 31 '15 at 21:32
  • If you use bash arithmetic test, you can skip the regex test: (( 10#$1 <= MAX )). With the forced use of base 10, only numbers using 0-9 digits should get through. – muru Dec 31 '15 at 21:36
  • @Serg I updated the question, could you please look at that. – Carter Roeser Jan 05 '16 at 05:28