写一个项目里词条对比的shell

需求:找出当前开发分支里未上传到词条平台的词条

#! /bin/bash
#-----------------------------------------------------------------------#
# 手动版说明:
# 1、将此shell放在项目根目录里跟.git目录平级;
# 2、如果查找的分支显示没有修改,有可能是已经合到master了,所以没有差异;
# 3、如果想自己查找某个目录里的所有词条,直接在相应目录执行 grep "\bt('[^']\+'"  -ro .|  awk -F "'" '{print $2}' | sort -u
#-----------------------------------------------------------------------#

# 对应平台的标识 aaa 3 bbb 7 ccc 60
# aaa | bbb | ccc
projectName=$(git remote -v |head -n 1 | awk '{print $2}' | sed 's/.*\///'| sed 's/\.git//')
case $projectName in
  "aaa")
      npid=3;
      ;;
  "bbb")
      npid=7;
      ;;
  "ccc")
      npid=60;
      ;;
  "exit")
      echo "异常:未知项目~";
      exit
      ;;
esac
echo "当前项目:$projectName"
# 输入查找的分支
read -p '请输入你所要查找的分支名:' branch
[ -z "$branch" ] && echo '异常:未输入分支名~' && exit;
# 获取系统最新版本
latestVersionRps=$(curl "https://xxxxxx?npid=${npid}")
function parseField() {
 intercept=`echo ${latestVersionRps#*\"$1\"\:}`
 fieldValue=`echo ${intercept%%\,*}`
}
parseField "version"
if [ ! -f /tmp/bbl_version ] || [ ! -s /tmp/bbl_version ] || [ `cat /tmp/bbl_version` != $fieldValue ]
  then
  # 保存最新版本号
  echo $fieldValue > /tmp/bbl_version;
  bblVersion=$(cat /tmp/bbl_version | awk -F '"' '{print $2}')
  # 保存所有词条
  curl "https://xxxxxx/${npid}/${bblVersion}/CN.json" --compressed | grep -Eo \"CN\"\:\".*?[^\"]\"\, | awk -F '"' '{print $4}' > "/tmp/tmp_all_words_on_bbl_file"
fi;

if [ -f "/tmp/tmp_all_words_on_bbl_file" ]
  then
  cat /dev/null > "/tmp/tmp_modified_files";
  cat /dev/null > "/tmp/tmp_words_file";
  cat /dev/null > "/tmp/tmp_uniq_words_file";
  if git rev-parse --verify $branch || git rev-parse --verify origin/$branch;
    then
      git fetch;
      git checkout $branch  || ! echo '异常:切换分支报错,请先暂存当前分支更改~' || exit
      git pull;
      git diff --name-only master...$branch > "/tmp/tmp_modified_files";
      for file in $(awk '{print $1}' "/tmp/tmp_modified_files")
      do
        [ -f "$file" ] && grep "\bt('[^']\+'"  -ro "$file"|  awk -F "'" '{print $2}' | sort -u  >> "/tmp/tmp_words_file";
      done
        cat "/tmp/tmp_words_file" | sort -u > "/tmp/tmp_uniq_words_file" ;
    else
       echo "异常:没找到该分支~";
       exit;
  fi;
  # 如果词条文件内容不为空
  if [ -s  "/tmp/tmp_uniq_words_file" ]
    then
    uniqWordsList=($(cat "/tmp/tmp_uniq_words_file"))
    allWordsOnBblList=($(cat "/tmp/tmp_all_words_on_bbl_file"))
    cat /dev/null > "/tmp/tmp_need_upload_words_list";
    declare -a needUploadWordsList
    index=0
    isExist=0;
    for uniqWordsListItem in "${uniqWordsList[@]}"
    do
        for allWordsOnBblListItem in "${allWordsOnBblList[@]}"
        do
            if [ "${uniqWordsListItem}" == "${allWordsOnBblListItem}" ]; then
                isExist=1
                break
            fi
        done
        if [[ $isExist -eq 0 ]]; then
            needUploadWordsList[index]=$uniqWordsListItem
            echo $uniqWordsListItem >> /tmp/tmp_need_upload_words_list
            index=$((index+1))
        else
            isExist=0
        fi
    done
      if [ ${#needUploadWordsList[@]} -gt 0 ];
      then
        result=$(cat "/tmp/tmp_need_upload_words_list")
        echo -e "\033[1;31m\n\n最终需要上传到bbl的词条汇总:\033[0m\n\033[33m$result\033[0m";
      else
        echo -e "\033[1;32m\n\nbbl平台上已存在该分支改动涉及的所有词条\033[0m"
      fi;
    else
     echo "异常:该分支没有修改~";
     exit;
  fi
fi;

  

posted @ 2022-07-01 21:17  韭菜茄子  阅读(5)  评论(0编辑  收藏  举报