為什麼我不能echo $1 > /sys/class/backlight/acpi_video0/brightness
在簡單的 bash 腳本中使用?
它給了我錯誤:echo: write error: Invalid argument
。
答案1
嘗試echo "$1" > /sys/class/backlight/acpi_video0/brightness
。
我敢打賭 shell 正在擴展 $1,因此 echo 認為它正在接收一堆參數,而不是一個字串。
答案2
該文件是一個特殊文件。如果寫入的內容不僅僅是數字,則無法寫入。如果您嘗試用 寫一個數字echo
,您將在末尾得到一個換行符。echo -n
解決了問題。
編輯:另外,你可能會遇到我剛才遇到的問題;你需要成為root用戶,sudo
無論出於何種原因都不會幫助你,這使得打字變得非常乏味su
; <你的命令>;exit
每時每刻。為此我製作了一個(過於雄心勃勃的)python 腳本:
#!/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()
答案3
您應該檢查 的實際值是多少$1
。此錯誤意味著您正在嘗試寫入無效值 - 要么超出範圍,要么通常不是一個有意義的值。
乍一看,它似乎接受 0 到 8 範圍內的整數(至少對我來說)。
答案4
嘗試使用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