bash-回显 : write error: invalid argument

标签 bash shell scripting echo sh

我是 bash 的新手,正在尝试编写一个禁用 kworker 业务的脚本,如 aMaia 的回答 here .

到目前为止,我有这个,我从 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 $1}' $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  

但是运行它会导致:

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

这是怎么回事?我以为 $i 只是文件名,这似乎是 echo 的正确语法。

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

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

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

最佳答案

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

解决方案(在 Bash 中):echo -n disable >/sys/firmware/acpi/interrupts/gpe66。这样就不会回显换行符。

关于bash-回显 : write error: invalid argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27734792/

相关文章:

string - 对于每个文件 : Walk line by line and grep for string (incorrect output)

bash - 检测远程安装

shell - 在 shell 中打印多行 - 提高配置 Vagrantfile 时的可读性

linux - "bash -c"命令有什么用?

linux - 创建一个脚本,将代码行添加到 .bashrc,然后重新加载终端

带空格的 Bash 变量

bash - 跨机器(VM、服务器等)同步终端设置

linux - Shell:如何将多个文件移动到一个目录并用不同的名称压缩该目录?

scripting - 以编程方式组合 SVG 图像

linux - 在 shell 脚本中编辑标准输入文件