Backlight script - should this have worked?

Forum rules
Before you post please read how to get help
Post Reply
tpprynn
Level 3
Level 3
Posts: 187
Joined: Fri May 27, 2011 7:09 pm

Backlight script - should this have worked?

Post by tpprynn » Sun Mar 27, 2016 3:20 pm

I have this in, for the time being, my home folder, with the chmod +x applied. I sewed it together from an amount of reading and things remembered from attempts to script a couple of years ago. If I'm honest it started to drive me mad then, so that I'm unlikely to learn this in competent depth for any length of time, but i would like to pull this off if I can.

I want the backlight to change in relation to whether I have my laptop's adaptor plugged in. It may be because I'm using LXDE but the built-in power management isn't working and this seems a known and unresolved issue. I'd like the backlight almost full on when using the psu, the maximum is 15 and 12 will be fine. On battery power 3 is ideal. So how is this?

***

#!/bin/bash

ac_adapter=$(acpi -a | cut -d' ' -f3 | cut -d- -f1)

if [ "$ac_adapter" = "on" ]; then
echo 12 > /sys/class/backlight/acpi_video0/brightness
else
echo 3 > /sys/class/backlight/acpi_video0/brightness
fi

***

...There is an Intel entry in the same sys/class/backlight folder and I wondered if this is some kind of clash that is making the built-in management not work. The numbers (much bigger seven or eight digit ones) in its files change according to use of the backlight keys as well.

As you might guess, the line with the word cut in is one I don't really get. I understand I need to learn about cron next to make this work best.

Thanks for any replies.

User avatar
LinuxJim
Level 5
Level 5
Posts: 659
Joined: Tue Jan 26, 2016 8:01 pm
Location: Oregon, USA

Re: Backlight script - should this have worked?

Post by LinuxJim » Sun Mar 27, 2016 3:37 pm

tpprynn wrote: As you might guess, the line with the word cut in is one I don't really get. I understand I need to learn about cron next to make this work best.
'cut' is like a pair of scissors - it 'cuts' bits and pieces out of whatever you feed it.

In this case, you're feeding it the output of 'acpi -a'. That will be one of:

Adapter 0: on-line
-or-
Adapter 0: off-line

The first cut, 'cut -d' ' -f3' says to cut out the third field (-f3), using ' ' (space) as a delimiter between fields.
The third field is one of:

on-line
-or-
off-line

The second cut, 'cut -d- -f1' gets the output of the first cut as input.
In this case, it says to use '-' (dash) as the delimiter and cut out the first field (-f1). So, you end up with:

on
-or-
off

tpprynn
Level 3
Level 3
Posts: 187
Joined: Fri May 27, 2011 7:09 pm

Re: Backlight script - should this have worked?

Post by tpprynn » Mon Mar 28, 2016 3:04 am

Thanks. acpi wasn't installed but things aren't solved yet. It looks like I don't need to understand cut, though I do now from the explanation given, but if I type acpi -a into a terminal it gives either on-line or off-line, with nothing needing to be cut. So shouldn't the following work? It isn't working yet. If I execute it on a one-off basis prior to proper install it should do something shouldn't it? I was hoping that with the plug pulled the brightness would switch to 2, and with it inserted it'd switch to 15. The ac adaptor does make a difference, but only in a tiny way relative to what setting there is at the point it's plugged in or removed.

#!/bin/bash

ac_adapter=$(acpi -a)

if [ "$ac_adapter" = "on-line" ]; then
echo 15 > /sys/class/backlight/acpi_video0/brightness
else
echo 2 > /sys/class/backlight/acpi_video0/brightness
fi

Thanks for any pointers.

BlackVeils
Level 5
Level 5
Posts: 589
Joined: Wed Nov 06, 2013 9:26 am

Re: Backlight script - should this have worked?

Post by BlackVeils » Sat Apr 23, 2016 6:32 am

you probably already have this sorted, but i will answer anyway.
yes that is better, there was no need for cut. because it edits a system file, it will need to be executed with root privileges. what you want is to have it in a loop, imagine it gets run at boot-up, then keeps running in the background all of the time, because its repeatedly checking. you could choose for it to check the status of acpi output every 15 second or whatever you want. example:

Code: Select all

#!/bin/bash

while true; do
  if acpi -a | grep -q "on-line"; then
    echo 15 > /sys/class/backlight/acpi_video0/brightness
  else
    echo 2 > /sys/class/backlight/acpi_video0/brightness
  fi
  sleep 15s
done
to start the script, add it to /etc/rc.local, your command would be something like this:

first put PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin near the top of rc.local, for environment.

"/path/to/your/script" &

you will need to store the script in /usr/local/bin or something.
i think that should work.

Post Reply

Return to “Scripts & Bash”