Does a do while loop exist?

Solved
niernier Posted messages 256 Status Membre -  
niernier Posted messages 256 Status Membre -
Hello,
I would like to know if there is a do while loop in bash (like in the C language) or if there is a way to mimic it.

While waiting for your response, thank you!
Configuration: Linux Firefox 3.0.17

2 réponses

scriptiz Posted messages 1494 Status Membre 425
 
Bash loops:

for loop
 #!/bin/bash for i in $( ls ); do echo item: $i done


while loop
 #!/bin/bash COUNTER=0 while [ $COUNTER -lt 10 ]; do echo The counter is $COUNTER let COUNTER=COUNTER+1 done


until loop
 #!/bin/bash COUNTER=20 until [ $COUNTER -lt 10 ]; do echo COUNTER $COUNTER let COUNTER-=1 done


There is no proper do ... while loop,
to create a do while loop, you just need to ensure that you enter the loop at least once; the until loop is handy for that, even though it is just the negation of the while.
--
When man points at the moon, the fool looks at the finger!
0
lami20j
 
Hello,

We can also do the for loop the C way ;-)

$ cat for.sh #!/bin/bash for ((i=0 ; $i < 10; i++)) do echo $i done $ sh for.sh 0 1 2 3 4 5 6 7 8 9
0