【BASH工具】使用 newBashFunc 创建新的bash函数

版权声明:本文为博主原创文章,欢迎转载,但请标出本文的出处,谢谢!欢迎访问本人的github主页【https://github.com/abodu/omb】 https://blog.csdn.net/liudglink/article/details/85005637
注: 另附函数 sf_getShfmt 用于从github里面下载安装最新版本的 shfmt(用于格式化bash脚本)
#!/usr/bin/env bash

sf_newBashFunc() {
  function tmpl() {
    cat
  } <<'NEW_BASH_FUNC_DEFINE'
#!/usr/bin/env bash
#=================================================================
# CPSTR: Copyright (c) 2018 By Abodu, All Rights Reserved.
# FNAME: TPLNAME.sh
# DESCP: TPLDESC
# AUTHR: abodu,[email protected]
# ENCOD: UTF-8 Without BOM
# VERNO: 0.0.1
#=================================================================

TPLNAME() {

  usage() {
    echo
    echo "$gfunc - TPLDESC" && echo
    echo "USAGE: $gfunc { [-h] }"
    echo
  }

  checkArg() {
    #ZAR[ "X$1" == "X" ] && usage && return 1
    while [ $# -ge 1 ];
    do
      case $1 in
        -[hH] | --[hH]elp)
          usage && return 1
          ;;
        *)
          :
          ;;
      esac
      shift
    done
    return 0
  }

  local lFuncs="usage checkArg "
  #when you want keep global clean,append self-defined function below here
  lFuncs="${lFuncs} "
  trap "unset ${lFuncs}" EXIT RETURN INT HUP

  local gfunc=$FUNCNAME
  checkArg $@
  [ $? -ne 0 ] && return
  ##add-main-logical-below-here

}

TPLNAME $@
NEW_BASH_FUNC_DEFINE

  function reformat() {
    /usr/bin/which shfmt &>/dev/null
    if [ $? -eq 0 ]; then
      cat $1 | shfmt -ln bash -i 2 -w $1
    else
      cat $1 | sed 's/  / /g'
    fi
  }
  
  function usage() {
    echo && echo "$gfunc - create a new bash function" && echo
    echo "USAGE: $gfunc [-r] [-p] [-n] <func_name> [func_desc]"
    echo
    echo "  -d,--desc         add function's description"
    echo "  -n,--no-entrance  without entrance, just for source "
    echo "  -r,--zar          when has zero args, will exit function: XXX"
    echo "  -p,--only-print   only print function's definition,not save to file: XXX.sh"
    echo
  }

  function checkArg() {
    [ $# -eq 0 ] && usage && return 1
    local notOptStr=""
    while [ $# -ge 1 ]; do
      case $1 in
      -[hH] | --[hH]elp)
        usage && return 1
        ;;
      -[pP] | --only-print)
        lOnlyPrint=1
        ;;
      -[rR] | --zar)
        lZeroArgReturn=1
        ;;
      -[nN] | --no-entrance)
        lNoEntrance=1
        ;;
      -[dD] | --desc)
        shift
        desc="$(echo $@ | sed 's! -[sS]!!g; s! --[sS]ave!!g')"
        ;;
      *)
        [ "X${1:0:1}" != "X-" ] && notOptStr="$notOptStr$1 "
        ;;
      esac
      shift
    done

    if [ "X$notOptStr" != "X" -a "X$full" == "X" ]; then
      full="${notOptStr%% *}"
      notOptStr="${notOptStr#* }"
    fi
    if [ "X$notOptStr" != "X" -a "X$desc" == "X" ]; then
      desc=$notOptStr
    fi

    return 0
  }

  #注册清理子函数的钩子,防止子函数污染全局定义域里的函数定义
  local lFuncs="usage checkArg "
  lFuncs="${lFuncs} reformat tmpl"
  trap "unset ${lFuncs}" EXIT RETURN INT HUP
  
  #定义外层函数的局部变量 - 相当于子函数的全局变量
  local gfunc=$FUNCNAME
  local full= desc=
  local lZeroArgReturn=${ZAR-0} optZAR='/#ZAR/d'
  local lNoEntrance=0 lOnlyPrint=0

  #校验参数
  checkArg $@
  [ $? -ne 0 ] && return

  if [ "X$full" == "X" ]; then
    echo && echo "needs function's name, re-run like this: '${gfunc} -n NEW_FUNC_NAME'"
    usage && return
  fi

  #实例化模板到指定的文件
  local rst=${full}.sh
  tmpl >$rst

  #根据参数定制化
  sed -i -e "s!TPLNAME!${full}!g" -e " s!TPLDESC!${desc}!g" $rst

  [ $lZeroArgReturn -eq 1 ] && optZAR='s!#ZAR!!g'
  sed -i "${optZAR}" $rst

  [ "X$lNoEntrance" = "X1" ] && sed -i '$d' $rst

  #格式化文件 需要使用shfmt 可以通过sf_getShfmt从github里面下载安装最新版本
  reformat $rst

  if [ $lOnlyPrint -eq 1 ]; then
    cat $rst | sed -n '10,$p'
    rm -rf $rst
  else
    cat $rst
  fi
}

sf_getShfmt() {
  
  local GREP='/usr/bin/grep'
  local CURL=$(/usr/bin/which curl) 

  local reposName="mvdan/sh"
  [ "X$1" != "X" ] && reposName="$1" && shift
  local tmpFile=/tmp/getShfmt_$$_$(date +"%s").txt
  # 通过github的API获取指定的项目的最新发布版本
  $CURL -L https://api.github.com/repos/${reposName}/releases/latest 2>/dev/null |\
    ${GREP} '"browser_download_url":' | ${GREP} '_amd64' | sed 's/"//g' | awk '{print $NF}' >$tmpFile

  #若获取不到,这直接跳过下载的步骤
  [ -s $tmpFile ] || return

  local downloadURL= cmd="$CURL -L -4 -o /bin/shfmt"
  case $OSTYPE in
  [lL]inux*)
    [ "$(id -u)" -ne 0 ] && cmd="sudo $cmd"
    downloadURL="$(cat $tmpFile | ${GREP} _linux)"
    ;;
  [dD]arwin*)
    downloadURL="$(cat $tmpFile | ${GREP} _darwin)"
    ;;
  [Mm]sys* | [Cc]ygwin*)
    downloadURL="$(cat $tmpFile | ${GREP} _windows)"
    cmd="${cmd}.exe"
    ;;
  esac
  
  if [ "X$downloadURL" == "X" ]; then
    echo && echo "Now Just Support Linux/Windows Mingw/Mac OS X, skip download shfmt" && echo
    return
  fi
  
  $cmd $downloadURL 2>/dev/null
  #下载成功,后续处理1: 调整程序的可执行权限
  if [ $? -eq 0 ]; then
    local destFile="${cmd##* }"
    [ -x $destFile ] || chmod a+x $destFile
  fi
}


猜你喜欢

转载自blog.csdn.net/liudglink/article/details/85005637