0

Is it possible to do do; condition; while loop in bash?

For example:

do
curl -f google.com/demo
while [ $? -ne 0 ]
done

If I do

while [ $? -ne 0 ]
do curl -f google.com/demo
done

...I depend on the command before the loop.

suren
  • 7,817
  • 1
  • 30
  • 51

1 Answers1

1

No, it's not possible.

The only valid alternative is to use break in an endless loop:

while :; do
   if ! curl -f google.com/demo
   then
       break
   fi
done

And anyway this looks like XY question and what you really want maybe is to loop until the command returns nonzero:

while ! curl -f google.com/demo; do :; done

or

until curl -f google.com/demo; do :; done
tripleee
  • 175,061
  • 34
  • 275
  • 318
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 2
    I assume he wants to repeat the command until the return value is 0: `until curl -f google.com/demo; do :; done` – Cyrus Sep 26 '19 at 18:03
  • @Cyrus Och, right, didn't think about it too long. – KamilCuk Sep 26 '19 at 18:13
  • I edited out [the `$?` antipattern](/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Sep 27 '19 at 06:01