bash嵌套循环在运行内循环之前运行通过外循环

时间:2017-10-13 17:07:53

标签: json bash loops for-loop

当执行bash脚本的以下部分时,它在循环通过内部数组之前循环遍历outter数组,或者至少是它看起来正在做的事情。

    for i in "${linkedTickets[@]}"
    do
        array=($( cat ${WORKDIR}/${i}_curl.log |
                egrep 'Depend' |
                sed 's/depends on//g; s/[][\"[:space:]]//g; s/fields,//g; s/issuelinks,//g; s/,type//g; s/,outward//g; s/outwarddependson//g; s/,name//g; s/Depend//g;' | tr -d '\040\011\015' | tr '\n' ' '))
        var=$i
        if [ ${#array[@]} -ne 0 ]; then
                echo ""
                echo "${!array[@]} --  ${array[@]}"
                echo "$var"
                for j in "${array[@]}"
                do
                        cat "$WORKDIR/${var}_curl.log" |
                                grep -R "\[\"fields\",\"issuelinks\",${j},\"outwardIssue\",\"fields\",\"status\",\"name\"\]" |
                                sort -u
                done
        fi
done

输出如下:

0 --  0
PROJ-198
PN/PROJ-16114/PROJ-1233_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]  "Closed"
PN/PROJ-16114/PROJ-188_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-198_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-204_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-209_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-1371_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]  "On Hold"

0 1 2 3 4 5 --  0 1 2 3 4 5
PROJ-186
PN/PROJ-16114/PROJ-16114_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"] "Closed"
PN/PROJ-16114/PROJ-1142_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]  "Closed"
PN/PROJ-16114/PROJ-172_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-180_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-182_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-185_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-186_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-187_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-188_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-198_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-204_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-209_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-1371_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]  "On Hold"
PN/PROJ-16114/PROJ-16114_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"] "Closed"
PN/PROJ-16114/PROJ-172_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-180_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-182_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-185_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-186_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-187_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-188_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-200_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Deployed"
PN/PROJ-16114/PROJ-201_curl.log:["fields","issuelinks",1,"outwardIssue","fields","status","name"]   "Deployed"
PN/PROJ-16114/PROJ-16114_curl.log:["fields","issuelinks",2,"outwardIssue","fields","status","name"] "Closed"
PN/PROJ-16114/PROJ-180_curl.log:["fields","issuelinks",2,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-182_curl.log:["fields","issuelinks",2,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-185_curl.log:["fields","issuelinks",2,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-186_curl.log:["fields","issuelinks",2,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-198_curl.log:["fields","issuelinks",2,"outwardIssue","fields","status","name"]   "Deployed"
PN/PROJ-16114/PROJ-199_curl.log:["fields","issuelinks",2,"outwardIssue","fields","status","name"]   "Deployed"
PN/PROJ-16114/PROJ-209_curl.log:["fields","issuelinks",2,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-16114_curl.log:["fields","issuelinks",3,"outwardIssue","fields","status","name"] "Closed"
PN/PROJ-16114/PROJ-182_curl.log:["fields","issuelinks",3,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-186_curl.log:["fields","issuelinks",3,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-209_curl.log:["fields","issuelinks",3,"outwardIssue","fields","status","name"]   "Deployed"
PN/PROJ-16114/PROJ-16114_curl.log:["fields","issuelinks",4,"outwardIssue","fields","status","name"] "Closed"
PN/PROJ-16114/PROJ-186_curl.log:["fields","issuelinks",4,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-16114_curl.log:["fields","issuelinks",5,"outwardIssue","fields","status","name"] "Closed"
PN/PROJ-16114/PROJ-186_curl.log:["fields","issuelinks",5,"outwardIssue","fields","status","name"]   "Deployed"

0 --  0
PROJ-209
PN/PROJ-16114/PROJ-16114_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"] "Closed"
PN/PROJ-16114/PROJ-1142_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]  "Closed"
PN/PROJ-16114/PROJ-172_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-180_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
PN/PROJ-16114/PROJ-1371_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]  "On Hold"

可能只是犯了一些愚蠢的错误。但是我在这里做的一些背景,我正在通过一个Jira票到这个bash脚本,它通过At​​lassian的版本2 api卷曲票并解析出上面的JSON,然后我抓住链接票将它们放在linkedTickets [@]中并在脚本中循环几次然后卷曲这些票据以生成我的curl日志,一旦完成我正在搜索依赖项,上面的两个循环我遇到的麻烦是只是搜索一下这个机票#的问题,然后在循环中循环$ i到$ j完成,而不是一次通过$ i,一直到$ j,然后再回到$ i 。这是在我应该在内部使用while循环时使用for循环的情况吗?

0 --  0
PROJ-189
PN/PROJ-16114/PROJ-189_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
0 --  0
PROJ-195
PN/PROJ-16114/PROJ-195_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
0 --  0
PROJ-200
PN/PROJ-16114/PROJ-200_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"]   "Closed"
0 --  0
PROJ-209
PN/PROJ-16114/PROJ-16114_curl.log:["fields","issuelinks",0,"outwardIssue","fields","status","name"] "Closed"

我期待任何反馈意见,希望我不会因为我的语法而受到严厉打击,因为我确信它最多可能是不正确的。

1 个答案:

答案 0 :(得分:0)

从你的循环输入中取出引号。

for i in "${a[*]}"; do echo $i; done
1 2 3

for i in ${a[*]}; do echo $i; done
1
2
3