0

Possible Duplicate:
Redirecting stdout to a file you don't have write permission on

I am creating a script to change the brightness of my laptop. I discovered that I can do this using

echo 1000  > /sys/class/backlight/intel_backlight/brightness

for example. But I must to do this as root, not with sudo command. Well, I created the file /usr/bin/brilho containing

echo "$1"  > /sys/class/backlight/intel_backlight/brightness

and now I can execute it with brilho 1000. But the problem is the permission. This does not work with sudo brilho 1000 neither brilho 100. Again I have to change to root.

So, I would like to know how to improve this to facilitate my job.

Regards and thanks.

Sigur
  • 2,411
  • 8
  • 34
  • 45
  • Thanks so much. I decided to use `sudo bash -c "..."` to do the job. But the first time, it ask password but after this, does not. Any idea? – Sigur Sep 05 '12 at 02:09
  • 1
    You could also look at [xbacklight](http://linux.die.net/man/1/xbacklight) to manage this for you... – jasonwryan Sep 05 '12 at 03:53
  • @Sigur `sudo` caches your credentials when you use it, so it won't ask for a password again for a while. You can use `sudo -k` to make it forget – Michael Mrozek Sep 05 '12 at 04:02

1 Answers1

1

To allow arbitrary user to change brightness, you could setup sudo, invoke visudo to launch the editor, and put the following line at the end:

username ALL=NOPASSWD: /usr/bin/tee /sys/class/backlight/intel_backlight/brightness

And the script will be:

echo $value | sudo tee /sys/class/backlight/intel_backlight/brightness

Which will no longer ask your password

daisy
  • 53,527
  • 78
  • 236
  • 383
  • `sudoedit` returns `usage: sudoedit [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user name|#uid] file ...`. I should open what file to add that line? – Sigur Sep 05 '12 at 11:39
  • @Sigur mistake, should be `visudo` – daisy Sep 05 '12 at 11:40
  • I'd added that line. It's working when root user but still is asking pass when normal user. Well, one more question: should I write `username` or `my_username`? – Sigur Sep 05 '12 at 11:52
  • @Sigur you need to replace that with your actual login name – daisy Sep 05 '12 at 12:00
  • It's not working. Should I reboot or closing terminal is enough? It's still asking pwd. – Sigur Sep 05 '12 at 12:03
  • @Sigur try again, code edited, just found tee was in `/usr/bin/tee` instead of `/bin/tee` on Ubuntu – daisy Sep 05 '12 at 12:06
  • Perfect! Thanks so much. It's working now. Best wishes. – Sigur Sep 05 '12 at 12:08