1. 程式人生 > >【BASH工具】使用 newBashFunc 建立新的bash函式

【BASH工具】使用 newBashFunc 建立新的bash函式

注: 另附函式 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
[email protected]
[ $? -ne 0 ] && return ##add-main-logical-below-here } TPLNAME [email protected] 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
[email protected]
| 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 [email protected] [ $? -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 }