【问题标题】:stderr not getting redirected to a file in Solaris shellscriptstderr 没有被重定向到 Solaris shellscript 中的文件
【发布时间】:2016-09-26 14:10:00
【问题描述】:

我已经编写了一个 shell 脚本(在 Solaris 平台上,如果这很重要的话),并且想要将 ZFS 命令的所有输出和错误重定向到一个文件。但是当我执行 shell 脚本时,错误消息仍然显示在提示符上,而不是重定向到文件。以下是shell脚本:

#!/usr/bin/sh        

`zfs <command>` 2>&1 | tee file.txt   #doesn't work    
##`zfs <command>` >> output.txt 2>&1  #doesn't work    

这将创建一个大小为 0 字节的 output.txt,并在命令提示符上显示错误消息。

 sudo ./testShell.sh    
cannot load key for '/tank/test1': incorrect key.    

请问有什么意见吗?

【问题讨论】:

  • 试试:zfs key -l -a 2&gt;&amp;1 | tee file.txt(没有反引号)。
  • 删除反引号后它工作了..谢谢!

标签: shell solaris stderr


【解决方案1】:

这并不符合人们的预期:

`zfs <command>` 2>&1 | tee file.txt

上述命令执行zfs &lt;command&gt;,从该命令收集标准输出并将该输出视为命令并尝试执行它。

很可能,您只想执行 zfs &lt;command&gt; 并将其输出重定向。在这种情况下:

zfs <command> 2>&1 | tee file.txt

【讨论】:

  • 感谢您的解释!
猜你喜欢
  • 2014-07-22
  • 2021-06-29
  • 2012-06-16
  • 1970-01-01
  • 2012-12-07
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多