为了账号安全,请及时绑定邮箱和手机立即绑定

如何在使用管道时将stderr写入文件?

如何在使用管道时将stderr写入文件?

倚天杖 2019-07-04 10:18:22
如何在使用管道时将stderr写入文件?我知道怎么用tee要写入输出(STDOUT)aaa.sh到bbb.out,同时仍将其显示在终端中:./aaa.sh | tee bbb.out我现在该怎么写STDERR到一个名为ccc.out,当它还在展示的时候?
查看完整描述

3 回答

?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

这可能对人们通过谷歌发现这一点很有用。只需取消您想要尝试的示例。当然,可以重命名输出文件。


#!/bin/bash


STATUSFILE=x.out

LOGFILE=x.log


### All output to screen

### Do nothing, this is the default



### All Output to one file, nothing to the screen

#exec > ${LOGFILE} 2>&1



### All output to one file and all output to the screen

#exec > >(tee ${LOGFILE}) 2>&1



### All output to one file, STDOUT to the screen

#exec > >(tee -a ${LOGFILE}) 2> >(tee -a ${LOGFILE} >/dev/null)



### All output to one file, STDERR to the screen

### Note you need both of these lines for this to work

#exec 3>&1

#exec > >(tee -a ${LOGFILE} >/dev/null) 2> >(tee -a ${LOGFILE} >&3)



### STDOUT to STATUSFILE, stderr to LOGFILE, nothing to the screen

#exec > ${STATUSFILE} 2>${LOGFILE}



### STDOUT to STATUSFILE, stderr to LOGFILE and all output to the screen

#exec > >(tee ${STATUSFILE}) 2> >(tee ${LOGFILE} >&2)



### STDOUT to STATUSFILE and screen, STDERR to LOGFILE

#exec > >(tee ${STATUSFILE}) 2>${LOGFILE}



### STDOUT to STATUSFILE, STDERR to LOGFILE and screen

#exec > ${STATUSFILE} 2> >(tee ${LOGFILE} >&2)



echo "This is a test"

ls -l sdgshgswogswghthb_this_file_will_not_exist_so_we_get_output_to_stderr_aronkjegralhfaff

ls -l ${0}


查看完整回答
反对 回复 2019-07-04
  • 3 回答
  • 0 关注
  • 822 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信