当前位置:网站首页>如何编写一个优雅的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
- el-pagination分页分页设置
- 【过一下15】学习 lstm的一周
- 第四讲 反向传播随笔
- 转正菜鸟前进中的经验(废话)之谈 持续更新中... ...
- 【过一下10】sklearn使用记录
- In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
- 关于基于若依框架的路由跳转
- 【过一下11】随机森林和特征工程
- Error creating bean with name 'configDataContextRefresher' defined in class path resource
猜你喜欢

机器学习(一) —— 机器学习基础

Flink Distributed Cache 分布式缓存

Mesos learning

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

flink项目开发-flink的scala shell命令行交互模式开发

Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent

The fourth back propagation back propagation

如何停止flink job

关于基于若依框架的路由跳转

pycharm中调用Matlab配置:No module named ‘matlab.engine‘; ‘matlab‘ is not a package
随机推荐
学习总结week2_3
[Go through 4] 09-10_Classic network analysis
Matplotlib(一)—— 基础
02.01-----The role of parameter reference "&"
【Untitled】
redis 持久化
【过一下15】学习 lstm的一周
【读书】长期更新
第四讲 back propagation 反向传播
el-table,el-table-column,selection,获取多选选中的数据
Lecture 3 Gradient Tutorial Gradient Descent and Stochastic Gradient Descent
Flink 状态与容错 ( state 和 Fault Tolerance)
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
flink yarn-session的两种使用方式
What are the characteristics of the interface of the physical layer?What does each contain?
In Opencv, imag=cv2.cvtColor(imag,cv2.COLOR_BGR2GRAY) error: error:!_src.empty() in function 'cv::cvtColor'
学习总结week2_4
[Remember 1] June 29, 2022 Brother and brother double pain
软件设计 实验四 桥接模式实验
Redux