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

  1. 记录日志,记录脚本开始执行时间、结束时间
  2. usage 函数,脚本需接参数执行,避免误执行,告诉用户,这个脚本的使用方法
  3. 加锁,创建锁文件,脚本不允许多人同时执行,或脚本未执行结束又开始执行,尤其计划任务或数据库备份,避免这种问题
    #!/bin/bash
    #######################################################
    # $Version: v1.
    # $Function: Shell Template Script
    # $Author: Jerry.huang
    # $organization: http://www.cnblogs.com/Mrhuangrui
    # $Create Date: -- :
    # $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=$
    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
    shell_log "mysql backup stop"
    shell_unlock
    } # Main Function
    main(){
    case $ in
    backup)
    mysql_backup
    ;;
    *)
    shell_usage;
    esac
    } #Exec
    main $

    shell_template.sh

SHELL 脚本小技巧的更多相关文章

  1. shell脚本小技巧

    输入参数错误时,退格会出现^H,这个时候只要在脚本顶部加一条语句:stty erase ^h就可以了 #!/bin/sh stty erase ^h

  2. Shell脚本小技巧收集

    1.使用python快速搭建一个web服务器 访问端口8000 python -m SimpleHTTPServer 2.获取文件大小 stat -c %s $file stat --printf=' ...

  3. linux运维自动化shell脚本小工具

    linux运维shell 脚本小工具,如要分享此文章,请注明文章出处,以下脚本仅供参考,若放置在服务器上出错,后果请自负 1.检测cpu剩余百分比 #!/bin/bash #Inspect CPU # ...

  4. shell脚本小案例

    1.获取远程ftp数据到本地目录 #!/bin/bash ftp -n<<! open 135.0.24.19 user exchange exchange binary cd /idep ...

  5. shell脚本常用技巧

    shell脚本常用技巧 1.获取随机字符串或数字 ~]#echo $RANDOM | md5sum | cut -c 1-6 ~]#openssl rand -base64 4 | cut -c 1- ...

  6. shell脚本小实例

    本文收集了一堆的shell脚本技巧,我说过,我写博客主要是作一些学习笔记,方便自己查阅,所以,我会搞出这么一篇文章,也没有什么不可理解的.关于这些技巧的出处,诶,我也忘了,可能来自theunixsch ...

  7. shell脚本使用技巧3--调试

    1.使用-x,开启shell脚本的跟踪调试功能 ex:bash -x script.sh or sh -x script.sh 2.使用set -x 和 set +x对脚本进行部分调试(输入中间的内容 ...

  8. shell脚本小集锦

    1) 如何向脚本传递参数 ? ./script argument 例子: 显示文件名称脚本 ./show.sh file1.txt cat show.sh #!/bin/bash 2) 如何在脚本中使 ...

  9. shell脚本调试技巧

    shell脚本调试之工具——bashdb http://www.cnblogs.com/itcomputer/p/5011845.html

随机推荐

  1. Nice Garland CodeForces - 1108C (思维+暴力)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  2. django之全局默认设置查看及admin语言设置

    django之admin语言设置 admin后台管理默认使用的是英文,有时我们需要将其设置成自己的语言以方便使用管理: 将 LANGUAGE_CODE = '' 设置为欲设置的语言即可. 以下为dja ...

  3. 【问题解决方案】从 Anaconda Prompt 或 Jupyter Notebook 终端进入Python后重新退出到命令状态

    从 Anaconda Prompt 或 Jupyter Notebook 终端进入Python后重新退出到命令状态 退出Python:exit() 或者 Ctrl+z 例子一枚 默认打开的是3.7,需 ...

  4. 【转】linux下查看磁盘分区的文件系统格式

    https://www.cnblogs.com/youbiyoufang/p/7607174.html

  5. C#封装SQLite数据库

    网上有许多介绍关于SQLite数据库的,这里我就不多说了,这里主要介绍SQLite数据库在C#中的应用,它的应用主要依赖于System.Data.SQLite.dll文件,可以点击这里下载https: ...

  6. mysql5.7以上安装

    下载:https://dev.mysql.com/downloads/mysql/ 1.在解压的mysql下(bin目录统计),创建my.ini 文件,内容日下(路径根据自己的目录修改) [mysql ...

  7. MySQL索引管理及执行计划

    一.索引介绍 二.explain详解 三.建立索引的原则(规范)

  8. python之路-字符串

    一.类型转换 a = 10 print(type(a)) # <class 'int'> d = str(a) # 把数字转换成str print(type(d)) # <class ...

  9. 安装使用阿里云的yum源

    CentOS 1.备份(备份本地Yum源) mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak 2.下 ...

  10. 老男孩python学习自修第十九天【异常处理】

    1.常见的错误 TypeError 类型错误 NameError 没有该变量 ValueError 不期望的值 AttributeError 没有该属性 UnboundLocalError 没有该局部 ...