zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Some good websites for C++
    Static Class in C#
    js提示后跳转代码集合
    日期格式化函数
    URL伪静态
    正则的一些使用
    提高.net网站的性能
    验证DropDownList的方法
    用C#去除字符串中HTML的格式
    drepdownlist不能动态绑定数据的原因
  • 原文地址:https://www.cnblogs.com/Mrhuangrui/p/7097453.html
Copyright © 2011-2022 走看看