2017-12-06 8 views
0

このコードを記述しようとすると、パイプ内でマップの終了が読み取られると、変数が1ずつ増えて最終的にwhileループから抜けるようにします。それ以外の場合は、キーファイルに固有のパラメータが追加されます。しかし、それは無限のループに入り、決してループから壊れることはありません。無限ループ中にスタックされています

while [ $a -le 5 ]; do 
    read input < map_pipe; 
    if [ $input = "map finished" ]; then 
      ((a++)) 
      echo $a 
    else 
      sort -u map_pipe >> keys.txt; 
    fi 
done 
+1

いくつか入力して所望の出力を提供します – PesaThe

+0

でスクリプトを確認してください[シェルチェック](http://shellcheck.net)。 – codeforester

答えて

0

私はこれがあなたが望んでいるかどうかわから、あなたのためにそれを修正しないことに決めたが、私は近いと思う:

#!/bin/bash 
a=0 #Initialize your variable to something 
while [ $a -le 5 ]; do 
    read input < map_pipe; 
    if [ "$input" = "map finished" ]; then #Put double quotes around variables to allow values with spaces 
     a=$(($a + 1)) #Your syntax was off, use spaces and do something with the output 
    else 
     echo $input >> keys.txt #Don't re-read the pipe, it's empty by now and sort will wait for the next input 
     sort -u keys.txt > tmpfile #Instead sort your file, don't save directly into the same file it will break 
     mv tmpfile keys.txt 
     #sort -u keys.txt | sponge keys.txt #Will also work instead of the other sort and mv, but sponge is not installed on most machines 
    fi 
done 
+0

そのコードでは、1つのマップ関数が実行された後にwhileループから壊れますが、5つのマップ関数を実行してループから抜け出したいのですか? – rhanly

+0

@あなたのコメントに従ってそれを変更しました – Veda