The bash limit and the loop are 10 attempts

How can this while loop be limited to a maximum of 10 attempts?

#!/bin/sh while ! test -d /somemount/share/folder do echo "Waiting for mount /somemount/share/folder..." sleep 1 done 
+5
source share
3 answers

Keep a counter:

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

You can also use the for loop and exit it with success:

 for try in {1..10} ; do [[ -d /somemount/share/folder ]] && break done 

The problem (which exists in other solutions too) is that as soon as the cycle ends, you don’t know how it ended - was the directory found or the counter ended?

+1
source

I would comment, but I do not have enough points for this. I still want to contribute. Thus, this makes it work even if the while loop is nested in another loop. before breaking, the variable c is reset to zero. @anubhava loans that came up with an 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 
+1
source

Source: https://habr.com/ru/post/1207892/


All Articles