当前位置:网站首页>如何编写一个优雅的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
边栏推荐
猜你喜欢

flink基本原理及应用场景分析

Lecture 5 Using pytorch to implement linear regression

DOM及其应用

Mesos学习

Mesos learning

【过一下12】整整一星期没记录
![[Let's pass 14] A day in the study room](/img/fc/ff4161db8ed13a0c8ef75b066b8eab.png)
[Let's pass 14] A day in the study room

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

Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent

Lecture 2 Linear Model Linear Model
随机推荐
小白一枚各位大牛轻虐虐
周末作业-循环练习题(2)
Detailed Explanation of Redis Sentinel Mode Configuration File
Flink Broadcast 广播变量
如何停止flink job
Matplotlib(三)—— 实践
[Remember 1] June 29, 2022 Brother and brother double pain
MySql之索引
【过一下7】全连接神经网络视频第一节的笔记
[Go through 3] Convolution & Image Noise & Edge & Texture
vscode要安装的插件
物理层的接口有哪几个方面的特性?各包含些什么内容?
Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution
es6迭代协议
day12函数进阶作业
In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
第二讲 Linear Model 线性模型
【过一下9】卷积
学习总结week3_3迭代器_模块
【After a while 6】Machine vision video 【After a while 2 was squeezed out】