问题:如何将Shell运行的程序结果保存,方便之后查看。
解决方式:
一、tee模块
在terminal端保存简单命令的结果
(1)覆盖日志文件
command | tee ./path/mylog.log
(2)将输出以追加的方式写入日志文件
command | tee -a ./path/mylog.log
注意 :
- 命令中的竖直线
|
为管道符号,详见管道符号 - 竖直线 | Shell -a
是append的缩写.log
是日志文件
在terminal端保存脚本运行的结果
# 直接覆盖的方式
script.sh 2>&1 | tee ./path/mylog.log
# 以追加的方式
script.sh 2>&1 | tee -a ./path/mylog.log
注意:
2>&1
将输出文件1和2合并
二、输出重定向方式
(1)覆盖的方式
command > ./test.txt
(2)追加的方式
command >> ./test.txt