使用方法:
将下面的代码保存到一文本里面,并使用 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