• SHELL 脚本小技巧


    脚本很简单,直接上功能介绍及脚本,可以做模板使用:

    1. 记录日志,记录脚本开始执行时间、结束时间
    2. usage 函数,脚本需接参数执行,避免误执行,告诉用户,这个脚本的使用方法
    3. 加锁,创建锁文件,脚本不允许多人同时执行,或脚本未执行结束又开始执行,尤其计划任务或数据库备份,避免这种问题
      #!/bin/bash
      #######################################################
      # $Version:      v1.0
      # $Function:     Shell Template Script
      # $Author:       Jerry.huang
      # $organization: http://www.cnblogs.com/Mrhuangrui
      # $Create Date:  2017-06-30 09:30
      # $Description:  You know what i mean,heiheihei
      #######################################################
      
      # Shell Env
      SHELL_DIR="/opt/shell"
      SHELL_LOG="${SHELL_DIR}/$0.log"
      LOCK_FILE="/tmp/$0.lock"
      
      #Write Log 
      shell_log(){
          LOG_INFO=$1
          echo "$(date "+%Y-%m-%d") $(date "+%H-%M-%S") : $0 : ${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
      mysql_backup(){
          if [ -f "$LOCK_FILE" ];then
              shell_log "$0 is running"
              echo "$0" is running,exit now. && 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)
                  mysql_backup
                  ;;
              *)
                  shell_usage;
          esac
      }
      
      #Exec
      main $1
      shell_template.sh
  • 相关阅读:
    逆序数———线段树/树状数组
    线段树 模板
    博弈论--对称博弈
    matlab程序设计
    matlab矩阵的操作
    2nd 历年学生作品评论(3部)
    1st 四人小组项目
    1st 本周工作量及进度统计
    1st 结对编程:简易四则运算
    1st 英文文章词频统计
  • 原文地址:https://www.cnblogs.com/Mrhuangrui/p/7097453.html
Copyright © 2020-2023  润新知