繁体   English   中英

卡在无限的while循环中

[英]Stuck in an infinite while loop

我正在尝试编写此代码,以便如果进程读取管道中的映射完成,则将变量增加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

我决定为您修复它,不确定这是否是您想要的,但是我想我很接近:

#!/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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM