在shell脚本循环中使用awk时,变量不会更新

时间:2013-09-26 02:39:57

标签: shell awk

我尝试创建一个脚本来生成git的提交ID列表。

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="trunk@${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="trunk@${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

1 个答案:

答案 0 :(得分: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