寫一個項目裏詞條對比的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;

  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章