【问题标题】:How to combine these two commands?: ./script.sh > logfile.log and ./script.sh 2> logfile.log [duplicate]如何组合这两个命令?:./script.sh > logfile.log 和 ./script.sh 2> logfile.log [重复]
【发布时间】:2016-08-19 22:44:55
【问题描述】:

如何组合这两个命令?

./script.sh > logfile.log 

./script.sh 2> logfile.log

谢谢!

【问题讨论】:

  • 你是什么意思结合?您希望他们的两个输出都进入日志文件吗?
  • 是两个不同的脚本吗?如果是这样,请使用:./s1.sh > log; ./s2.sh 2>> log 请参阅附加 >>
  • 这是一个脚本,我确实希望它们的两个输出都进入日志文件

标签: bash logging logfile


【解决方案1】:

在 bash 中,要将标准输出和标准错误重定向到同一个文件,可以这样写

./script.sh &> logfile.log

如果你想兼容其他shell,

./script.sh > logfile.log 2>&1

【讨论】:

  • 第一个只适用于bash 4+
  • 当您记录时,您可能需要附加日志文件而不是覆盖它,因此只需更改为./script.sh >> logfile.log 2>&1
  • 谢谢!第二个选项完美)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
  • 2012-09-02
相关资源
最近更新 更多