当前位置:网站首页>如何编写一个优雅的Shell脚本(二)
如何编写一个优雅的Shell脚本(二)
2022-08-05 05:14:00 【IT_xhf】
简介
继上一篇,本文主要描述一个shell脚本的基本组成
公共模块
## 自定义写日志函数
function writelog() {
echo "[`date -d '%Y-%m-%d %H:%M:%S'`][$$]: $1" |tess -a $tmplogfile
}
# 脚本的核心业务程序应该使用该函数去调用,控制程序的运行状态和日志管理
function EXESH_CMD() {
writelog "execute [$CMD] begining ...."
eval $CMD 1>&2 > $tmplogfile
cat $tmplogfile >> $logfile
ret=$?
if [ $ret -ne 0 ]; then
# 如果返回状态不为0 ,则程序出现了错误,将错误日志直接打印终端,并退出程序
cat $tmplogfile
exit 1
else
# 状态码为0 表示程序运行成功,成功,直接打印
writelog "finished [$CMD]."
fi
}脚本头部
#!/bin/sh
#############################################################
## 文件:xxxx
## 描述:xxxx
## 日期:yyyy-MM-dd
## 作者:xxxxx
############################################################
########################修改记录#############################
#日期 版本 作者 修改描述
#2018-11-04 V01.000 xxx 描述修改内容。。。
#................
###########################################################
VERSION="V01.000"
tmplogfile="/tmp/result.$$.`date+%S`.log"
logfile=`basename $0`.log
脚本公共模块
####################公共模块########################
## 加载自定义环境变量
source /path/common.sh
###################################################脚本主体
#####################脚本主体################################
writelog "Program starting....(Version=$VERSION)"
CMD="核心脚本"
EXESH_CMD
CMD="核心脚本"
EXESH_CMD
.....
#############################################################脚本末尾
writelog "Program Finished"
exit 0
边栏推荐
- Returned object not currently part of this pool
- 【过一下 17】pytorch 改写 keras
- flink项目开发-flink的scala shell命令行交互模式开发
- [Decoding tools] Some online tools for Bitcoin
- [Go through 7] Notes from the first section of the fully connected neural network video
- Flink Broadcast 广播变量
- Transformation 和 Action 常用算子
- [Study Notes Dish Dog Learning C] Classic Written Exam Questions of Dynamic Memory Management
- ES6 Set、WeakSet
- el-pagination分页分页设置
猜你喜欢

解决:Unknown column ‘id‘ in ‘where clause‘ 问题

Lecture 4 Backpropagation Essays

【过一下6】机器视觉视频 【过一下2被挤掉了】

Matplotlib(三)—— 实践

Calling Matlab configuration in pycharm: No module named 'matlab.engine'; 'matlab' is not a package

el-pagination左右箭头替换成文字上一页和下一页

vscode+pytorch使用经验记录(个人记录+不定时更新)

The fourth back propagation back propagation

【After a while 6】Machine vision video 【After a while 2 was squeezed out】

flink基本原理及应用场景分析
随机推荐
【过一下11】随机森林和特征工程
Distributed and Clustered
【过一下15】学习 lstm的一周
1.3 mysql batch insert data
Detailed Explanation of Redis Sentinel Mode Configuration File
第四讲 反向传播随笔
Matplotlib(三)—— 实践
对数据排序
Flink EventTime和Watermarks案例分析
【读书】长期更新
el-table,el-table-column,selection,获取多选选中的数据
Machine Learning (2) - Machine Learning Fundamentals
Flink和Spark中文乱码问题
[Go through 10] sklearn usage record
ES6 生成器
A blog clears the Redis technology stack
In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
数据库期末考试,选择、判断、填空题汇总
ES6基础语法
学习总结week3_2函数进阶