单选题
运维工程师在RHEL 9上编写脚本,需要把命令输出同时写入日志文件并显示在终端,且不覆盖原日志内容。下列写法正确的是?
A. command >> app.log
B. command 2> app.log
C. command > app.log
D. command | tee -a app.log
正确答案:D
依据 tee(1) 手册,tee 将标准输入同时写到文件和标准输出,-a 表示追加;> 与 >> 只重定向不显示,2> 仅重定向标准错误,均无法同时满足终端显示与追加写日志。
单选题
运维人员小陈在RHEL 9上编写服务健康检查脚本,需要把命令curl -s http://127.0.0.1/health的标准输出和标准错误都重定向到文件health.log,且错误输出覆盖写入。下列重定向写法正确的是?
A. curl -s http://127.0.0.1/health > health.log 2> health.log
B. curl -s http://127.0.0.1/health 2> health.log 1>&2
C. curl -s http://127.0.0.1/health &> health.log 2>&1
D. curl -s http://127.0.0.1/health > health.log 2>&1
正确答案:D
bash(1)规定>file 2>&1先把标准输出重定向到文件,再让标准错误复制标准输出的目标;2>file 1>&2顺序会导致标准输出仍到终端,两次>同一文件还会截断覆盖。