代码改变世界

shell脚本模版

2012-05-30 10:36 zhenjing 阅读(...) 评论(...) 编辑 收藏
#!/bin/bash

PATH=/usr/local/mysql/bin:/usr/local/bin:/bin:/usr/bin

# ========== CONFIG ==================

#source $(dirname $0)/config.sh # load public config under the same dir
CONF
=/tmp/conf RST_DIR=/tmp # ============DEBUG================== # for debug DEBUG=0 function Exec { echo $1 if [ $DEBUG -eq 0 ] then ($1) fi } LAST=$# # get last parameter if [[ ${!LAST} == "d" || ${!LAST} == "debug" ]] then echo ${!LAST} DEBUG=1 fi # ======================================== function SH_Usage() { echo "Usage: $0 DATE" echo "Example: $0 20111111" exit 1 } if (($# < 1)) then SH_Usage fi typeset readonly CUR_DATE=$(date --date="$1" +%Y%m%d); if [ "${CUR_DATE}" != "$1" ] then echo "Error Date: $1" >&2 SH_Usage; fi typeset readonly RST_DATE=${CUR_DATE} CUR_DATE=`date --date="$1" +%Y%m%d` RST_DATE=${CUR_DATE} # ========================================= echo "Start of $0 $@ ..."`date "+%Y-%m-%d %X"` cmd="rm $RST_DIR/*" Exec "$cmd" echo "End $0 $@."`date "+%Y-%m-%d %X"`

 

模版的各部分介绍:

1) 配置 -- 定义脚本所需的各种常量;

2)DEBUG -- 支持脚本调试

3)脚本参数判断、获取日期等常规操作

4)脚本主体