2017-12-06 10 views
0

프로세스가 맵에서 맵을 읽으면 변수가 1 씩 증가하여 결국 while 루프에서 빠져 나올 수 있도록이 코드를 작성하려고합니다. 그렇지 않으면 고유 한 매개 변수가 키 파일에 추가됩니다. 그러나 그것은 무한 루프로 들어가고 절대로 루프에서 빠져 나오지 않습니다.무한 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

을 원하는 [ shellcheck] (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

그래서 그 코드로 하나의 맵 함수가 실행 된 후에 while 루프에서 빠져 나오지 만 5 개의 맵 함수를 실행하고 루프에서 빠져 나오고 싶습니까? – rhanly

+0

@rhanly 귀하의 의견에 따라 그것을 바 꾸었습니다. – Veda