2013-09-26 3 views
0

커밋 ID 양식 git 목록을 생성하는 스크립트를 만들려고했습니다.쉘 스크립트 루프 내에서 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

관련 문제