bash - 回显:写入错误:参数无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27734792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:48:40  来源:igfitidea点击:

bash - echo: write error: invalid argument

bashshellscriptingechosh

提问by nicole

I am new to bash and trying to write a script that disables kworker business as in aMaia's answer here.

我是 bash 的新手,并试图编写一个脚本来禁用 kworker 业务,如 aMaia 的回答here

So far, I have this, which I run from root:

到目前为止,我有这个,我从 root 运行:

  1 #!/bin/bash                                                                      
  2                                                                                  
  3 cd /sys/firmware/acpi/interrupts                                                 
  4 for i in gpe[[:digit:]]* # Don't mess with gpe_all                               
  5 do                                                                               
  6     num=`awk '{print }' $i`                                                    
  7     if (( $num >= 1000 )); then  # potential CPU hogs?                           
  8         # Back it up and then disable it!!                                       
  9         cp $i /root/${i}.backup                                                  
 10         echo "disable" > $i                                                      
 11     fi                                                                           
 12 done  

But running it results in:

但是运行它会导致:

./kkiller: line 10: echo: write error: Invalid argument

What is going on here? I thought $iwas just the file name, which seems like the correct syntax for echo.

这里发生了什么?我以为$i只是文件名,这似乎是 echo 的正确语法。

Suggestions for cleaning up/improving the script in general are also appreciated!

也非常感谢有关清理/改进脚本的建议!

Update: With set -vxadded to the top of the script, here is a problematic iteration:

更新:set -vx添加到脚本的顶部,这是一个有问题的迭代:

+ for i in 'gpe[[:digit:]]*'
awk '{print }' $i
++ awk '{print }' gpe66
+ num=1024908
+ ((  1024908 >= 1000  ))
+ cp gpe66 /root/gpe66.backup
+ echo disable
./kkiller: line 10: echo: write error: Invalid argument

采纳答案by MajorT

I think it has something to with permissions. I don't think root has write access to those files by default. Try echoing manually 'disable' to that file, even as root you get the same error shown. So to make your script work, first do chmod 744 on $i before your echo, it should do the trick.

我认为它与权限有关。我认为默认情况下 root 没有对这些文件的写访问权限。尝试手动回显“禁用”到该文件,即使以 root 身份显示相同的错误。所以为了让你的脚本工作,在你的回声之前先在 $i 上执行 chmod 744 ,它应该可以解决问题。

回答by Adam Wallner

I had this problem too in Docker on Alpine linux environment. I think the problem is that echo by default put a newline character at the end of the string, and the kernel not accept it, but it is not the case in every system. In Docker I had this error, but the value was written despite the error message.

我在 Alpine linux 环境中的 Docker 中也遇到了这个问题。我认为问题是echo默认在字符串的末尾放了一个换行符,内核不接受它,但并不是每个系统都这样。在 Docker 中,我遇到了这个错误,但尽管有错误消息,但还是写入了值。

The solution (in Bash): echo -n disable >/sys/firmware/acpi/interrupts/gpe66. This way no newline is echoed.

将该溶液(在击): echo -n disable >/sys/firmware/acpi/interrupts/gpe66。这样就不会回显换行符。

回答by Chris Dunder

Double-check all spelling. echo "disabled" will emit a write error even for root whereas echo "disable" succeeds.

仔细检查所有拼写。echo "disabled" 即使对于 root 也会发出写入错误,而 echo "disable" 成功。

回答by Peter Nowee

I got the same error message trying to disable a GPE that was already set to disabled:

我在尝试禁用已设置为禁用的 GPE 时收到相同的错误消息:

# echo "disable" > /sys/firmware/acpi/interrupts/gpe17
-su: echo: write error: Invalid argument
# cat /sys/firmware/acpi/interrupts/gpe17
 3718289     STS disabled     unmasked

After enabling it, I could disable it without an error:

启用它后,我可以禁用它而不会出错:

# echo "enable" > /sys/firmware/acpi/interrupts/gpe17
# echo "disable" > /sys/firmware/acpi/interrupts/gpe17
#