2013-09-26 75 views
0

我試圖創建一個腳本來生成提交id表格git的列表。當在shell腳本中使用awk時,變量不會得到更新循環

START_INDEX=0 
while [ ${START_INDEX} -lt ${#VERSION_ARRAY[@]} ] 
do 
    CURRENT_COMMIT_LINE=$(some code) 
    NEXT_COMMIT_LINE=$(some code) 
    COMMIT_SHA_ARRAY[${START_INDEX}]=$(git log master | \ 
    awk -v start_line=${CURRENT_COMMIT_LINE}\ 
    -v end_line=${NEXT_COMMIT_LINE}\ 
    'NR == start_line, NR == end_line {if(match($0, "commit")) print $2}') 
    START_INDEX=$((START_INDEX+1)) 
done 

問題是變量「start_line」和「end_line」不會更新。 我試圖回顯「CURRENT_COMMIT_LINE」和「NEXT_COMMIT_LINE」,並通過循環過程進行更新。 我做錯了什麼?

整個腳本:

#!/bin/sh 
BACKUP_BRANCH_TAILING="_patch_backup" 
NEW_CODE_BCH_VERSION=$(git branch | awk -F '_' '/new_code.*[0-9]$/{print $3}') 
echo "New code version ${NEW_CODE_BCH_VERSION}" 
LATEST_SVN_VERSION=$(git log master | awk '/trunk\@/{first=match($2, "@")+1; s=substr($2, first, length($2)); print s; exit;}') 
echo "The latest svn version at master branch is ${LATEST_SVN_VERSION}" 
NUMBER_OF_PATCH_TO_APPLY=$((LATEST_SVN_VERSION-NEW_CODE_BCH_VERSION)) 
if [ ${LATEST_SVN_VERSION} -gt ${NEW_CODE_BCH_VERSION} ] ; then 
    #make sure the branch is switched to new_code_XXX 
    if [ -z "$(git branch | grep -i "^* new_code_.*[0-9]$")" ] ; then 
     echo "Switching to new_code branch..." 
     #git checkout new_code_${NEW_CODE_BCH_VERSION} 
    fi 

    #create a backup branch witht the tailing of patch_backup 
    if [ -z "$(git branch | grep -i "new_code_.*${BACKUP_BRANCH_TAILING}$")" ] ; then 
     echo "Creating backup..." 
     #git branch new_code_${NEW_CODE_BCH_VERSION}${BACKUP_BRANCH_TAILING} 
    fi 

    #create an array of version which are to be patched to the new_code branch 
    START_INDEX=0 
    echo "Debug: start index, ${START_INDEX}, end index ${END_INDEX}" 
    while [ ${START_INDEX} -lt ${NUMBER_OF_PATCH_TO_APPLY} ] 
    do 
     VERSION_ARRAY[${START_INDEX}]=$((NEW_CODE_BCH_VERSION+START_INDEX+1)) 
     echo "start index for creating version array${START_INDEX} verion ${VERSION_ARRAY[${START_INDEX}]}" 
     START_INDEX=$((START_INDEX+1)) 
    done 

    #create an array of commit ids associated with respective version 
    #START_INDEX=0 
    #while [ ${START_INDEX} -lt ${#VERSION_ARRAY[@]} ] 
    for ((START_INDEX=0; ${START_INDEX} < ${#VERSION_ARRAY[@]}; ++START_INDEX)); 
    do 
     #Find out the range between two commits 
     CURRENT_COMMIT_LINE=$(git log master | awk -v pat="[email protected]${VERSION_ARRAY[${START_INDEX}]}" ' $0 ~ pat {print NR}') 
     if [ $((START_INDEX+1)) -lt ${#VERSION_ARRAY[@]} ] ; then 
      NEXT_COMMIT_LINE=$(git log master | awk -v pat="[email protected]${VERSION_ARRAY[$((START_INDEX+1))]}" '$0 ~ pat {print NR}') 
     else 
      NEXT_COMMIT_LINE=1 
     fi 
     echo "current line: ${CURRENT_COMMIT_LINE}, newer line: ${NEXT_COMMIT_LINE}" 
     #Find the commit id between two commit 
     COMMIT_SHA_ARRAY[${START_INDEX}]=$(git log master |awk -v start_line="${NEXT_COMMIT_LINE}" \ 
             -v end_line="${CURRENT_COMMIT_LINE}" \ 
             'BEGIN {print "[BEGIN]:start line:"start_line ", end line:" end_line} \ 
             NR == start_line, NR== end_line {if(match($0, "commit")) print $2}') 

     echo "start index for creating version array${START_INDEX} commit ID: ${COMMIT_SHA_ARRAY[${ARRAY_INDEX}]}" 
     #START_INDEX=$((START_INDEX+1)) 
    done 

    #patch each commit id 

    #rename the branch name 

else 
    echo "There is no newer commits at the master branch!" 
fi 

下面是結果的一小部分

current line: 66, newer line: 58 
start index for creating version array14 commit ID: [BEGIN]:begin line:184, end line:192 
af9e83e393b1f627d8d16a1c43c3a365b8ed5aec 
current line: 58, newer line: 50 
start index for creating version array15 commit ID: [BEGIN]:begin line:184, end line:192 
af9e83e393b1f627d8d16a1c43c3a365b8ed5aec 
current line: 50, newer line: 42 
start index for creating version array16 commit ID: [BEGIN]:begin line:184, end line:192 
af9e83e393b1f627d8d16a1c43c3a365b8ed5aec 
current line: 42, newer line: 34 
start index for creating version array17 commit ID: [BEGIN]:begin line:184, end line:192 
af9e83e393b1f627d8d16a1c43c3a365b8ed5aec 
current line: 34, newer line: 26 
start index for creating version array18 commit ID: [BEGIN]:begin line:184, end line:192 
af9e83e393b1f627d8d16a1c43c3a365b8ed5aec 
current line: 26, newer line: 18 
start index for creating version array19 commit ID: [BEGIN]:begin line:184, end line:192 
af9e83e393b1f627d8d16a1c43c3a365b8ed5aec 
current line: 18, newer line: 7 
start index for creating version array20 commit ID: [BEGIN]:begin line:184, end line:192 
af9e83e393b1f627d8d16a1c43c3a365b8ed5aec 
current line: 7, newer line: 1 
start index for creating version array21 commit ID: [BEGIN]:begin line:184, end line:192 
af9e83e393b1f627d8d16a1c43c3a365b8ed5aec 
+0

好吧,我在我的調試消息中使用了不存在的變量時犯了一個錯誤。因此,我一直在獲得相同的SHA。儘管如此,我在BEGIN中打印start_line和end_line變量。我總是得到相同的結果。儘管如此,我得到了正確的SHA。 – user2817665

回答

1

你沒有正確附上命令替換:

COMMIT_SHA_ARRAY[${START_INDEX}]=$(git log master | \ 
awk -v start_line=${CURRENT_COMMIT_LINE})\ 
-v end_line=${NEXT_COMMIT_LINE}\ 
'NR == start_line, NR == end_line {if(match($0, "commit")) print $2}' 

它應該是:

COMMIT_SHA_ARRAY[${START_INDEX}]=$(git log master | awk -v start_line=${CURRENT_COMMIT_LINE} \ 
-v end_line=${NEXT_COMMIT_LINE} \ 
'NR == start_line, NR == end_line {if(match($0, "commit")) print $2}') 

將命令的一部分移動到下一行時要小心,因爲可能會將兩個參數合併爲一個。

另一個簡單的版本:

for ((START_INDEX = 0; START_INDEX < ${#VERSION_ARRAY[@]}; ++START_INDEX)); do 
    CURRENT_COMMIT_LINE=$(some code) 
    NEXT_COMMIT_LINE=$(some code) 
    COMMIT_SHA_ARRAY[START_INDEX]=$(git log master | awk -v start_line="${CURRENT_COMMIT_LINE}" \ 
     -v end_line="${NEXT_COMMIT_LINE}" \ 
     'NR == start_line, NR == end_line {if(match($0, "commit")) print $2}') 
done 

更新:

上線

echo "start index for creating version array${START_INDEX} commit ID: ${COMMIT_SHA_ARRAY[${ARRAY_INDEX}]}" 

ARRAY_INDEX大概應該是START_INDEX

+0

嗨konsolebox,我的道歉,那是我的錯字。我在那裏有一個封閉的支架。 bash不會抱怨任何語法錯誤。 thnx在下一行部分的建議:) – user2817665

+0

@ user2817665我不確定這是否因爲你在這裏結束了你的括號而只是一個錯字:'awk -v start_line = $ {CURRENT_COMMIT_LINE})' – konsolebox

+0

是的,這是我的壞。我的腳本在不同的機器上,所以我不能複製並粘貼到網站上。再次,我很抱歉,它讓你感到困惑 – user2817665