分享Shell模板

作者: ropon 分类: Shell 发布时间: 2018-08-23 09:16
#!/bin/bash
rsname=Template #shell名称
rsver=1.0 #shell版本号
base_dir=/usr/local #运行初始目录
mirrorLink=https://www.idiyrom.com #下载地址
flag=$1  #参数

#格式化显示函数
Echo()
{
	case $1 in
	    success)  flag="\033[1;32m"
	    ;;
	    failure)  flag="\033[1;31m"
	    ;;
	    warning)  flag="\033[1;33m"
	    ;;
	    msg)  flag="\033[1;34m"
	    ;;
	    *)  flag="\033[1;34m"
	    ;;
	esac
	if [[ $LANG =~ [Uu][Tt][Ff] ]]
	then
		echo -e "${flag}${2}\033[0m"
	else
		echo -e "${flag}${2}\033[0m" | iconv -f utf-8 -t gbk
	fi
}
#调用方法
#绿色 Echo "success" "成功信息"
#蓝色 Echo "msg" "提示信息"
#红色 Echo "failure" "错误信息"
#黄色 Echo "warning" "警告信息"

Check_OS() {
	if [ -n "$(grep 'Aliyun Linux release' /etc/issue)" -o -e /etc/redhat-release ]; then
		OS=CentOS
		[ -n "$(grep ' 7\.' /etc/redhat-release 2> /dev/null)" ] && CentOS_RHEL_version=7
		[ -n "$(grep ' 6\.' /etc/redhat-release 2> /dev/null)" -o -n "$(grep 'Aliyun Linux release6 15' /etc/issue)" ] && CentOS_RHEL_version=6
		[ -n "$(grep ' 5\.' /etc/redhat-release 2> /dev/null)" -o -n "$(grep 'Aliyun Linux release5' /etc/issue)" ] && CentOS_RHEL_version=5
	elif [ -n "$(grep 'Amazon Linux AMI release' /etc/issue)" -o -e /etc/system-release ]; then
		OS=CentOS
		CentOS_RHEL_version=6
	fi
} 

#shell介绍信息
header()
{
printf "
###################################################################
#  $rsname version $rsver Author: Ropon <idiyrom.com>          #
#  For more information please visit https://idiyrom.com/83.html  #
#-----------------------------------------------------------------#
#  Copyright @2017-2018 idiyrom.com. All rights reserved.         #
###################################################################

"
}

#帮助提示
showhelp()
{
	header
	Echo "msg" "Usage: $rsname [OPTIONS]"
	echo
	Echo "msg" "OPTIONS:"
	Echo "msg" "-h | --help : Show help of $rsname"
	Echo "msg" "-u | --update : update Check for $rsname"
	Echo "msg" "-i | --install : install $rsname version $rsver to This System"
	Echo "msg" "-U | --uninstall : Uninstall $rsname from This System"
	echo
}

#下载子函数
Download_src() {
	[ -s "${src_url##*/}" ] && Echo "msg" "[${src_url##*/}] found" || { wget --tries=6 -c --no-check-certificate $src_url; sleep 1; } #判断下载是否已存在
	if [ ! -e "${src_url##*/}" ]; then
		Echo "msg" "${src_url##*/} download failed, Please check!"
		kill -9 $$
	fi
}

#调用方法
#src_url=资源URL地址 && Download_src

Install()
{
	pushd ${base_dir}/src
	Echo "success" "安装完成"
	popd
}

Uninstall()
{
	
	Echo "success" "卸载完成"
}

Update()
{
	Echo "msg" "暂未开通"
}

#主函数,脚本入口
Main()
{
	case $flag in
		'-h' | '--help' | '?' )
			showhelp
			exit
				;;
			'--install' | '-i' )
				Install
				exit
				;;
			'--uninstall' | '-U' )
				Uninstall
				exit
				;;
			'--update' | '-u' )
				Update
				exit
				;;	
			* )
				showhelp
				exit
				;;
		esac
}

Main

 

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注