此脚本mkscript.sh用于快速创建bash初始脚本,定义编写脚本规范

使用说明:

mkscript.sh -f|--file FILE -a|--author AUTHOR -t|--time DATETIME -d|--description DESCRIPTION -v|--version VERSION -h|--help

选项说明:

-f|--file :指定创建的脚本文件路径,创建空脚本文件或编辑已存在的脚本文件

-a|--author :指定创建该脚本的作者

-t|--time :指定创建时间,默认为当前日期

-d|--description :描述脚本的用途或功能信息

-v|--version :脚本版本信息

-h|--help :脚本的使用说明


mkscript.sh脚本如下:

#!/bin/bash
# Author: ikki
# Date: 2013-08-01
# Description: make shell script templet
# Version: 1.0
HELP=`echo -e "Usage:./mkscript.sh -f|--file /PATH/TO/FILE -a|--author AUTHOR -t|--time -d|--description DESCRIPTION -v|--version VERSION OR Usage:./mkscript.sh -h|--help"`
if [ $# -gt 0 ]; then
  case $1 in
  -h|--help)
    if [ $# -eq 1 ]; then
      echo $HELP
      exit 0
    fi
    ;;
  -f|--file)
    if [ ! -d `dirname $2` ]; then
      echo "`dirname $2` is not exist."
      exit 3
    elif [ ! -s $2 ]; then
      echo "#!/bin/bash" > $2
      chmod +x $2
      SHFILE=$2
      shift 2
      for I in `seq 0 $#`; do
        if [ $# -gt 0 ]; then
          case $1 in
          -a|--author)
            echo "# Author: $2" >> $SHFILE
            shift 2
            ;;
          -t|--time)
            echo "# Date: `date +'%Y-%m-%d'`" >> $SHFILE
            shift
            ;;
          -d|--description)
            echo "# Description: $2" >> $SHFILE
            shift 2
            ;;
          -v|--version)
            echo "# Version: $2" >> $SHFILE
            shift 2
            ;;
          *)
            echo $HELP
            exit 4
            ;;
          esac
        fi
      done
    elif [ -s $2 ] && [ `cat $2 | head -1` != "#!/bin/bash" ]; then
      echo "The file is not a bash script."
      exit 5
    else
      vim + $2
    fi
    ;;
  *)
    echo $HELP
    exit 6
    ;;
  esac
else
  echo $HELP
  exit 7
fi