while ループの 'in' に予期しないマーカーの近くに構文エラーがあります。

while ループの 'in' に予期しないマーカーの近くに構文エラーがあります。

どのデータを処理したいのですが、テキストファイルに書かれた座標を読み取って処理する領域を絞り込む必要があります。

次のエラーがあります。

./script5.sh: line 59: syntax error near unexpected token'./script5.sh: 行 59:while IFS="" read -r $L1Aname north south east west || [[ -n "$L1Aname north south east west" ]] in $coord; do'

これが私が持っているものです:

while IFS="" read -r $L1Aname north south east west || [[ -n "$L1Aname north south east west" ]] in $coord; do
    Nlat="$north"  #name variable north
    Slat="$south"  #name variable south
    Elon="$east"  #name variable east
    Wlon="$west" #name variable west
done < "$coord"; 

ありがとうございます!

ベストアンサー1

Barmarが質問の説明で指摘したように、whileループはもともと繰り返しforループであるように見えます$coord(1つの変数がファイルの内容全体を保持できる)。

正しいwhileループは次のとおりです。

while read -r L1Aname north south east west; do
    Nlat="$north"
    Slat="$south"
    Elon="$east"
    Wlon="$west"
done <"$coord"

$私もあきらめました$L1Aname。これが正しいかどうかは全く確信できませんが、皆さんのようにできる read $L1Aname(これを行うと、変数に値が読み込まれます。誰の名前変数L1Aname)に保存されます。私はこれが意図していないと仮定します(L1Aname私が間違っている場合は下に変更してください)。$L1Aname

NULL以外の値を確認する必要がある場合は、文字列をテストしないでください。文字列"$L1Aname north south east west"はnullではないことが保証されています。代わりに個々の変数の値をテストしてください。

while read -r L1Aname north south east west
    && [ -n "$north" ] && [ -n "$south" ]
    && [ -n "$east"  ] && [ -n "$west"  ]
do
    Nlat="$north"
    Slat="$south"
    Elon="$east"
    Wlon="$west"

    # use "$Nlat", "$Slat", "$Elon" and "$Wlon" here.

done <"$coord"

$L1Aname包含が保証されているため、テストする必要はありません。彼らがread何かを読むことができれば。

おすすめ記事