1分钟教会你标准的shell脚本

作者: ropon 分类: Shell 发布时间: 2017-04-14 14:50
#!/bin/bash
#######################################################
# $Name:         shell_template.sh
# $Version:      v1.0
# $Function:     Introduce Function Template Script
# $Author:       Ropon
# $organization: https://www.west.cn
# $Create Date:  2016-1-20
# $Description:  You know what i mean,hehe
#######################################################
# Shell Env
SHELL_NAME="shell_template.sh"
SHELL_DIR="/root"
SHELL_LOG="${SHELL_DIR}/${SHELL_NAME}.log"
LOCK_FILE="/tmp/${SHELL_NAME}.lock"
#Write Log 
shell_log(){
    LOG_INFO=$1
    echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") : ${SHELL_NAME} : ${LOG_INFO}" >> ${SHELL_LOG}
}
# Shell Usage
shell_usage(){
    echo $"Usage: $0 {backup}"
}
shell_lock(){
    touch ${LOCK_FILE}
}
shell_unlock(){
    rm -f ${LOCK_FILE}
}
# Backup MySQL All Database with mysqldump or innobackupex
funcname(){
    if [ -f "$LOCK_FILE" ];then
        shell_log "${SHELL_NAME} is running"
        echo "${SHELL_NAME}" is running && exit
    fi
    shell_log "mysql backup start"
    shell_lock
    sleep 10
    shell_log "mysql backup stop"
    shell_unlock
}
# Main Function
main(){
    case $1 in
        backup)
            funcname
            ;;
        *)
            shell_usage;
    esac
}
#Exec
main $1

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