Does a do while loop exist?
Solved
niernier
Posted messages
256
Status
Membre
-
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!
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
Bash loops:
for loop
while loop
until loop
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!
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!
We can also do the for loop the C way ;-)