【BASH工具】标准的bash函数的创建模板

本文介绍了一个用于简化Bash函数创建过程的脚本工具。该工具通过命令行参数定义新函数的名称、描述和作者信息,自动生成符合标准格式的Bash函数模板,并支持即时预览或直接保存为文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用方法:

将下面的代码保存到一文本里面,并使用 source 命令加载后 直接执行 sf_newBashFunc 可查看具体用法

#!/usr/bin/env bash
#=================================================================
# CPSTR: Copyright (c) 2019 By Abodu, All Rights Reserved.
# FNAME: bse_nbf.sh
# AUTHR: abodu,abodu@qq.com
# CREAT: 2019-05-06 11:32:43
# ENCOD: UTF-8 Without BOM
# VERNO: 0.0.1
# LUPTS: 2019-07-03 12:03:53
#=================================================================

sf_newBashFunc() {
  lf_tmpl() {
    cat
  } <<'DEFINE_NEW_BASH_FUNC_TMPL'
#!/usr/bin/env bash
#=================================================================
# CPSTR: Copyright (c) TPLYEAR By Abodu, All Rights Reserved.
# FNAME: TPLNAME.sh
# DESCP: TPLDESC
# AUTHR: TPLCONTACT
# ENCOD: UTF-8 Without BOM
# CREAT: TPLCRATED
# VERNO: 0.0.1
# LUPTS: TPLCRATED
#=================================================================

TPLNAME() {

  lf_helper() {
    echo
    echo "TPLNAME - TPLDESC" && echo
    echo "Usage:"
    echo " $HT -h,--help   # print this help information"
    echo
  }

  trap "unset -f lf_helper" EXIT RETURN INT HUP

  # ===== define variables may be used in while-loop =====
  local HT="$FUNCNAME"

  # -- parse args
  while [ $# -ge 0 ]; do
    case $1 in
      "" | -h | --help | help ) lf_helper && return;;
      *) : #anyelse to do with ;;
    esac
    shift
    [ $# -eq 0 ] && break
  done

  # ===== other logic statements to add =====  
}

case $0 in
  *TPLNAME.sh) TPLNAME $@ ;;
esac

DEFINE_NEW_BASH_FUNC_TMPL

  lf_reformat() {
    local fmter=$(\which shfmt 2>/dev/null)
    if [ "X$fmter" != "X" ]; then
      cat $1 | $fmter -ln bash -i 2 -w $1
    else
      cat $1 | sed 's/  / /g'
    fi
  }

  lf_usage() {
    echo
    [ "X$1" == "X" ] && echo "$HT - create a new bash function" && echo
    cat
  } <<USAGE_PRINT
Usage:
  $HT [-p] <[-n] FUNC_FULLNAME> [[-d] FUNC_DESC] [ -c author,author@email ]  # define new bash function: FUNC_FULLNAME

Where:
  -n, --func-name  # function's name, if not has '-n', FUNCNAME is the first arg
  -d, --func-desc  # function's description mesages, if not has '-d', it be all remain args excepts the first one
  -p, --only-print # only display function's definition, not save it into file FUNC_FULLNAME.sh

USAGE_PRINT

  trap "unset -f lf_{tmpl,usage,reformat}" EXIT RETURN INT HUP
  local HT="$FUNCNAME" newFuncName= newFuncDesc= prtOnly=0

  while [ $# -ge 0 ]; do
    case $1 in
    "" | -[hH] | --[hH]elp) lf_usage && return ;;
    -[pP] | --only-print) prtOnly=1 ;;
    -[nN] | --func-name) shift && newFuncName=$1 ;;
    -[cC] | --contract) shift && NBF_CONTRACT="$1" ;;
    -[dD] | --func-desc)
      shift
      local dt=
      for dt in $@; do
        case $dt in
        -[pPnNcC] | --only-print | --func-name | --contract)
          break
          ;;
        *) newFuncDesc="${newFuncDesc}$dt " && shift ;;
        esac
      done
      [ $# -ge 1 ] && continue || break
      ;;
    *)
      if [ "X${1:0:1}" != "X-" ]; then
        [ "X$newFuncName" == "X" ] && newFuncName=$1 || newFuncDesc="$newFuncDesc$1 "
      fi
      ;;
    esac
    shift
    [ $# -eq 0 ] && break
  done

  if [ "X$newFuncName" == "X" ]; then
    echo
    echo "[FATAL] need a function's name, re-run likes below:"
    lf_usage 'no-summary-title'
    return
  fi

  local resultFile=/tmp/nbf.$(date +'%s').txt
  lf_tmpl >$resultFile

  local curYear=$(date +"%Y") curDatetime=$(date +"%F %T")
  sed -i -e "s!TPLNAME!${newFuncName}!g" \
    -e "s!TPLCRATED!${curDatetime}!g" \
    -e "s!TPLCONTACT!${NBF_CONTRACT-abodu,abodu@qq.com}!g" \
    -e "s!TPLYEAR!${curYear}!g" $resultFile
  if [ "X$newFuncDesc" == "X" ]; then
    sed -i '/TPLDESC/d' $resultFile
  else
    sed -i "s!TPLDESC!${newFuncDesc}!g" $resultFile
  fi

  lf_reformat $resultFile
  if [ $prtOnly -eq 1 ]; then
    cat $resultFile | tail -n +10
  else
    cat $resultFile >${newFuncName}.sh
  fi
  rm -f $resultFile
}

#just autoexecute when not source
case $0 in
*nbf.sh) sf_newBashFunc $@ ;;
*) alias nbf='sf_newBashFunc' ;;
esac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值