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
.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityTry 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.
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()
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).
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
function brightness { bright=$1; sudo su -c 'echo "$bright" > /sys/class/backlight/acpi_video0/brightness'; }
too, but I still haven't figured it out.