Linux Bash shell 和 Windows Bat CMD 完成共同功能的一些记录

Git 提交前替换某些目录内代码,提交后又恢复它

还包括一些数组操作,遍历目录,文本替换等功能

Linux 版本

#!/bin/bash

# usage:
# git commit: ./push.sh 'commit message'
# set nacos group to DEFAULT_GROUP: ./push.sh set default
# set nacos group to my name: ./push.sh set my


modules=("gigi-auth" "gigi-gateway" "gigi-visual/gigi-monitor")
for i in $( ls -d gigi-modules/*/)  #只显示目录
do
  modules+=("$i")   # 添加元素到数组后面
done

loopModules() {
  for i in "${!modules[@]}";
  do
    changeGroup "${modules[$i]}" "$1"
    # echo "${modules[$i]}"
  done
}

changeGroup(){
  filename=$1/src/main/resources/bootstrap.yml
  groupName=$(git config user.name)
  myGroupStr="group: $groupName"
  defaultGroupStr="group: DEFAULT_GROUP"

  if [ "$2" == "default" ]; then
    echo "------ $1 恢复为 $defaultGroupStr 参数 ---------"
    sed -i "" "s/$myGroupStr/$defaultGroupStr/g" "$filename"
  fi

  if [ "$2" == "my" ]; then
     echo "------ $1 更新为 $myGroupStr 参数 ---------"
     sed -i "" "s/$defaultGroupStr/$myGroupStr/g" "$filename"
  fi

}

gitPush() {
    useBranch="dev"
    echo "------ git checkout $useBranch ---------"
    git checkout "$useBranch"

    loopModules default

    echo "------ 提交本分支 ---------"
    echo "------ git add --all ---------"
    git add --all
    echo "------ git commit -m $commitMessage ---------"
    git commit -m "$commitMessage"
    echo "------ git pull origin $useBranch ---------"
    git pull origin "$useBranch"
    echo "------ git push origin $useBranch ---------"
    git push origin "$useBranch"

    loopModules my
}


commitMessage=$1
if [ "$commitMessage" == "" ] ; then
  echo "请输入 commit 信息"
  exit
elif [ "$commitMessage" == "set" ] ; then
  if [ "$2" == "default" ] ; then
    loopModules default
  elif [ "$2" == "my" ]; then
    loopModules my
  fi
else
  gitPush
fi




######  以下内容是根据标识行来增加或删除内容
# updateGroup "${modules[$i]}" "add"
# updateGroup "${modules[$i]}" "remove"
updateGroup(){
  groupName=$(git config user.name)
  groupStr="group: $groupName"
  filename=$1/src/main/resources/bootstrap.yml
  markStr='# 服务注册地址'
  markStrLine=$(awk "/$markStr/{ print NR; exit }" "$filename")   # 找出 '服务注册地址' 所在的行数
  insertLine=$(($markStrLine+2))  # 行数+2
  # echo "$markStrLine"
  # echo "$insertLine"
  # currentPath=$(pwd)

  readInsertLine=$(sed -n "${insertLine}p" "$filename") # 读取改行的字符串
  echo "$readInsertLine"

  if [[ "$readInsertLine" == *"group"* ]]; then   # 对比改行是否含有 group 字符串
    if [ "$2" = "remove" ]; then
      echo "------ $1 删除 group 参数 ---------"
      sed -i "" -e "${insertLine}d" "$filename"     # 删除改行
    else
      echo "------ $1 已经有 group 参数 ---------"
    fi
  else
    if [ "$2" = "add" ]; then
       echo "------ $1 插入 group 参数 ---------"
       sed -i "" -e "${insertLine}i\\
        $groupStr
        " "$filename"
    fi
  fi
}

Windows 版本

windows 新版本的语法与老版本有点不同,在循环体中用 !var! 代替 %var%
:: usage:
:: git commit: ./push.sh 'commit message'
:: set nacos group to DEFAULT_GROUP: ./push.sh set default
:: set nacos group to my name: ./push.sh set my

@echo off
setlocal enabledelayedexpansion


:: get which modules need to change group
SET modules=gigi-auth gigi-gateway gigi-visual\gigi-monitor
list the folds of gigi-modules
(for /f %%i in ('dir .\gigi-modules\ /ad /b') do (
  SET modules=!modules!;gigi-modules\%%i
))


:: set group config string
(for /f %%i in ('git config user.name') do (
  SET groupName=%%i
))
echo "my group name is %groupName%"
SET myGroupStr="group: %groupName%"
SET defaultGroupStr="group: DEFAULT_GROUP"


:: set git commit message
SET commit_msg=%1
IF "%commit_msg%"=="" (
  echo "Please input commit message"
  exit
) ELSE IF "%commit_msg%"=="set" (
  IF "%2"=="default" (
    CALL :loopModules default
  ) ELSE IF "%2"=="my" (
    CALL :loopModules my
  )
) ELSE (
  CALL :gitPush
)



EXIT /B %ERRORLEVEL%
:loopModules
(for %%m in (%modules%) do (
  :: echo %%m
  CALL :changeGroup %%m %1
))
EXIT /B 0


:changeGroup
SET filename=%1\src\main\resources\bootstrap.yml
IF "%2"=="default" (
  echo "------ %1 reset to %defaultGroupStr% ---------"
  powershell -Command "(gc %filename% -encoding utf8) -replace '%myGroupStr%', '%defaultGroupStr%' | Out-File -encoding utf8 %filename%"
) ELSE IF "%2"=="my" (
  echo "------ %1 set to %myGroupStr% ---------"
  powershell -Command "(gc %filename% -encoding utf8) -replace '%defaultGroupStr%', '%myGroupStr%' | Out-File -encoding utf8 %filename%"
)
EXIT /B 0


:gitPush
SET useBranch=dev
echo "------ git checkout %useBranch% ---------"
git checkout %useBranch%

CALL :loopModules default

echo "------ git add --all ---------"
git add --all
echo "------ git commit -m %commit_msg% ---------"
git commit -m "%commit_msg%"
echo "------ git pull origin %useBranch% ---------"
git pull origin %useBranch%
echo "------ git push origin %useBranch% ---------"
git push origin %useBranch%

CALL :loopModules my

EXIT /B 0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值