0

おそらくばかげた質問ですが、while abort の理由がわかりません。

私はこのようなファイルを持っています:

"id": "00000000000000000",
"visibilitystate": 1,
"profilestate": 8
"somethingelse": "abc",
"id": "99999999999999999",
"againsomethingelse": "cba"
"visibilitystate": 0,
"profilestate": 9

100万回以上の繰り返し、番号、指定、および値は、idとidの間で異なる場合がありますが、idは常に異なります。私の最初の考えは、ループで読み取られ、値を配列に格納し、後でmysql-dbに挿入することでした。

私はこれを試します:

set -x
#Data extract array
array=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

#Control array
array2=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)

while read a; do
    if [[ ${array2[0]} == 1 ]]; then
        awk '/"id":/ { exit 42 }'
        if [[ $? -eq 42 ]]; then
            echo mysql
            array2[0]=0
        fi
    fi
    if [[ ${array2[0]} == 0 ]]; then
        awk '/"id":/ { exit 42 }'
        if [ $? -eq 42 ]; then
            array[0]=`sed -n 's/.*"id":."\(.*\)",.*/\1/p'`
            array2[0]=1
        fi
    fi
done <testf

set +x

1行読んだ後、ループ終了、理由がわかりません。

出力:

+ array=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
+ array2=(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)
+ read a
+ [[ 0 == 1 ]]
+ [[ 0 == 0 ]]
+ awk '/"id":/ { exit 42 }'
+ '[' 42 -eq 42 ']'
++ sed -n 's/.*"id":."\(.*\)",.*/\1/p'
+ array[0]=
+ array2[0]=1
+ read a
+ set +x

誰かが私を助けることができますか?

4

1 に答える 1

2

There are 3 problems in the script. You are using 3 different commands inside the loop that are reading from stdin (awk twice and sed).

When you use awk '/"id":/ { exit 42 }' it reads everything else (the input file). You probably want to apply that command in $a. That would be something like:

echo $a | awk '/"id":/ { exit 42 }'

The second error is the same, but in the next if.

The third error is similar, but in this case when you use sed. You probably want to do something like:

array[0]=`echo $a | sed -n 's/.*"id":."\(.*\)",.*/\1/p'`

By the way, test is a reserved word in bash, you should avoid using it as file name (not related with your problem, though).

于 2012-10-15T01:39:10.693 に答える