Linux下知识点整理(二)

Faster R-CNN日志自动生成脚本中知识点分析:

#!/bin/bash

set -x
set -e

...

# #是shell中的注释
# 按照 年-月-日_时-分-秒格式
LOG="xxx.txt.`date + '%Y-%m-%d_%H-%M-%S'`"
exec &> >(tee -a "$LOG")
echo Logging output to "$LOG"

...

知识点一:set -x

输出调试信息

知识点二:set -e

每个脚本文件开头都应该加上set -e,这句语句告诉bash如果任何语句的执行结果不是true则应该退出。这样的好处是防止错误像滚雪球般变大导致一个致命的错误,而这些错误本应该在之前就被处理掉。如果要增加可读性,可以使用set -o errexit,它的作用与set -e相同。

知识点三:IO重定向

  • I/O重定向通常与 FD有关,shell的FD通常为10个,即 0~9;

  • 常用FD有3个,为0(stdin,标准输入)、1(stdout,标准输出)、2(stderr,标准错误输出),默认与keyboard、monitor、monitor有关;

知识点四: exec &>

exec &> >(tee -a "$log_file")
echo This will be logged to the file and to the screen

exec &> >(...)会将 标准输出、和标准错误输出到...中;exec > ...只将标准输出输出到脚本中;tee -a ... 将标准输入追加到文件,并且中在屏幕上输出

参考:
* Unix/Linux 脚本中 “set -e” 的作用
* linux exec用法总结
* Shell脚本调试技术
* Using exec and tee to redirect logs to stdout and a log file in the same time [duplicate]

猜你喜欢

转载自blog.csdn.net/u010472607/article/details/78846725