Limit bash while loop to 10 retries

前端 未结 4 1361
滥情空心
滥情空心 2021-01-12 07:34

How can this while loop be limited to maximum 10 retries?

#!/bin/sh
while ! test -d /somemount/share/folder
do
    echo \"Waiting for mount /somemount/share/         


        
4条回答
  •  梦毁少年i
    2021-01-12 07:48

    I would comment but I do not have enough points for that. I want to contribute anyway. So this makes it work even if the while loop is nested in another loop. before the break the c variable is being reset to zero. credits to @anubhava who came up with the original solution.

    #!/bin/sh
    
    while ! test -d /somemount/share/folder
    do
        echo "Waiting for mount /somemount/share/folder..."
        ((c++)) && ((c==10)) && c=0 && break
        sleep 1
    done
    

提交回复
热议问题