当前位置:网站首页>如何编写一个优雅的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
边栏推荐
- 学习总结week2_4
- 【过一下10】sklearn使用记录
- Machine Learning (2) - Machine Learning Fundamentals
- 【过一下4】09-10_经典网络解析
- 学习总结week3_1函数
- day6-列表作业
- Transformation 和 Action 常用算子
- Returned object not currently part of this pool
- vscode+pytorch use experience record (personal record + irregular update)
- Detailed Explanation of Redis Sentinel Mode Configuration File
猜你喜欢
pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
Lecture 2 Linear Model Linear Model
Mesos学习
解决:Unknown column ‘id‘ in ‘where clause‘ 问题
Lecture 5 Using pytorch to implement linear regression
【过一下10】sklearn使用记录
[Let's pass 14] A day in the study room
coppercam primer [6]
Flink EventTime和Watermarks案例分析
【过一下12】整整一星期没记录
随机推荐
2022 The 4th C.Easy Counting Problem (EGF+NTT)
[Remember 1] June 29, 2022 Brother and brother double pain
【过一下6】机器视觉视频 【过一下2被挤掉了】
【过一下10】sklearn使用记录
el-pagination分页分页设置
Mesos学习
小白一枚各位大牛轻虐虐
[Go through 7] Notes from the first section of the fully connected neural network video
【过一下14】自习室的一天
【读书】长期更新
coppercam primer [6]
门徒Disciples体系:致力于成为“DAO世界”中的集大成者。
flink部署操作-flink standalone集群安装部署
浅谈Servlet生命周期
【Reading】Long-term update
A blog clears the Redis technology stack
将照片形式的纸质公章转化为电子公章(不需要下载ps)
[Software Exam System Architect] Software Architecture Design ③ Domain-Specific Software Architecture (DSSA)
Map、WeakMap
【After a while 6】Machine vision video 【After a while 2 was squeezed out】