#!/bin/bash
export LANG=zh_CN.UTF-8
author: zhizhimao <zhizhimao.vip.qq.com>
filename: vish
version: v.0.0
date:2020-01-30 13:14:00
option: -d 描述 -v 指定版本
describe: 创建脚本添加作者、文件名、版本、日期、描述内容,检查语法是否正确,并实现版本控制自动添加版本号。打开非自定义脚本文件时备份后打开;
Version=v.0.0
Date=$(date "+%F %T")
create_sh(){
创建脚本文件
cat > $File << EOF
#!/bin/bash
export LANG=zh_CN.UTF-8
author: zhizhimao <zhizhimao.vip.qq.com>
filename: $Filename
version: $Version
date:$Date
option:
describe: $Desc
EOF
}
check(){
until bash -n $File_ver; # 检查语法,语法错误重新打开修改
do
read -p "Syntax error; 输入x退出,回车继续编辑!" Str
case $Str in
x)
echo "Exit!" && exit 2;;
*)
vi + $File_ver;;
esac
done
}
while getopts ":d:v:" Opt; # 第一‘:‘不显示错误信息,第二‘:‘d参数后面跟内容
do
case $Opt in
d)
Desc=$OPTARG;; # getopts $OPTARG内置变量,显示参数后面跟内容
v)
Version=$OPTARG;;
*)
echo "无效选项! $1"
echo "Usage: [-d DESCRIPTION] [-v VERSION ] filename"
echo vish -d ‘描述内容‘ -v v.0.1 test.sh;;
esac
done
[[ $# -gt 1 ]] && shift $((OPTIND-1)) # getopts $OPTIND内置变量,参数指针,删除位置参数
Filename=$(basename $1)
Directory=$(dirname $1)
File=$1
[[ ! -s $File ]] && create_sh && vi + $File && chmod +x $File && exit 0
if [ -s $File ];
then
Author=$(sed -n ‘3p‘ $File |awk ‘{print $3}‘)
Name=$(basename -s .sh $File |awk -F‘_‘ ‘{print $1}‘)
if [ -z $Author ];
then
cp $File "$Directory/$Filename.$Date"
vi + $File
elif [ $Author == ‘zhizhimao‘ ];
then
if [ $Version != v.0.0 ];
then
Filever=$Directory/$Name\$Version.sh
cp $File $File_ver
sed -i "5 s/version:./version: $Version/g" $File_ver
else
Ver=$(sed -n ‘5p‘ $File |awk ‘{print $3}‘)
Num=$(sed -n ‘5p‘ $File |awk ‘{print $3}‘ |awk -F‘.‘ ‘{print $3}‘)
Num_1=$(($Num +1 ))
Ver_sion=${Ver%.}.$Num_1
Filever=$Directory/$Name\$Ver_sion.sh
cp $File $File_ver
sed -i "5 s/$Ver/$Ver_sion/g" $File_ver
fi
sed -i "6 s/date:.*/date: $Date/g" $File_ver
[[ -n $Desc ]] && sed -i "8 a# $Desc" $File_ver
vi + $File_ver
check
else
cp $File "$Directory/$Filename.$Date"
vi + $File
fi
fi
原文地址:https://blog.51cto.com/zhizhimao/2468684