4

Why can't I use echo $1 > /sys/class/backlight/acpi_video0/brightness in a simple bash script?

It gives me the error: echo: write error: Invalid argument.

  • Having the same issue while trying to do the same thing. I've tried things like function brightness { bright=$1; sudo su -c 'echo "$bright" > /sys/class/backlight/acpi_video0/brightness'; } too, but I still haven't figured it out. – hangtwenty Nov 2 '12 at 12:15
2

Try echo "$1" > /sys/class/backlight/acpi_video0/brightness.

I bet the shell is expanding $1 and thus echo thinks it is receiving a bunch of arguments, rather than a string.

0

You should check what the actual value of $1 is. This error means you are trying to write an invalid value -- either it's out of range or just in general not a meaningful value.

At a glance, it appears that it accepts an integer in the range 0 to 8 (for me at least).

0

Try using let

#!/bin/bash

POLKU='/sys/class/backlight/radeon_bl0/brightness'


if [ $# -eq "0" ]
    then
        echo 100 > $POLKU
    else
        let gg=$1
        echo $gg > $POLKU
fi
0

That file is a special file. It cannot be written to if what is written is not solely a number. If you try writing a number with echo, you will get a newline character at the end. echo -n solves the problem.

EDIT: Also, you might having the problem which I just had; that you need to be root and sudo won't help you for whatever reason, making it very tedious to type su; <your command>; exit all the time. For this I made an (overly ambitious) python script:

#!/usr/bin/python

from sys import *

PATH = "/sys/class/backlight/intel_backlight/brightness"

if len(argv) != 2:
    print("Usage: bright.py <brightness>")
    exit()

try:
    brightness = int(argv[1])
    if not 0 <= brightness <= 825:
        raise Exception()
except:
    print("<brightness> must be an integer between 0 and 825.")
    exit()

if brightness == 0:
    readString = raw_input("A value of 0 will turn off your screen. Are you sure you want to continue? [y/N] ")
    if readString != "y":
        exit()
elif brightness <= 5:
    with open(PATH, "r") as f:
        oldBrightness = int(f.read())
        if brightness < oldBrightness:
            readString = raw_input("A value of %i will make your screen very dark. Are you sure you want to continue? [y/N] " % brightness)
            if readString != "y":
                exit()

try:
    with open(PATH, "w") as f:
        f.write(str(brightness))
except:
    print("Failed to write to file. Are you root?")
    exit()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.