Skip to main content
additional information
Source Link
Dennis Williamson
  • 356k
  • 93
  • 379
  • 442

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested
while (($?))
do
    # do something until it returns 0    
done

Or you can emulate a do while loop:

while 
    # do some stuff
    # do some more stuff
    # do something until it returns 0    
do
    continue  # just let the body of the while be a no-op
done

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

For completeness, exit and return each accept an optional argument which is an integer (positive, negative or zero) which sets the return code as the remainder of the integer after division by 256. The current shell (or script or subshell*) is exited using exit and a function is exited using return.

Examples:

$ (exit -2); echo "$?"
254
$ foo () { return 2000; }; foo; echo $?
208

* This is true even for subshells which are created by pipes (except when both job control is disabled and lastpipe is enabled):

$ echo foo | while read -r s; do echo "$s"; exit 333; done; echo "$?"
77

Note that it's better to use break to leave loops, but its argument is for the number of levels of loops to break out of rather than a return code.

Job control is disabled using set +m, set +o monitor or shopt -u -o monitor. To enable lastpipe do shopt -s laspipe. If you do both of those, the exit in the preceding example will cause the while loop and the containing shell to both exit and the final echo there will not be performed.

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested
while (($?))
do
    # do something until it returns 0    
done

Or you can emulate a do while loop:

while 
    # do some stuff
    # do some more stuff
    # do something until it returns 0    
do
    continue  # just let the body of the while be a no-op
done

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested
while (($?))
do
    # do something until it returns 0    
done

Or you can emulate a do while loop:

while 
    # do some stuff
    # do some more stuff
    # do something until it returns 0    
do
    continue  # just let the body of the while be a no-op
done

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

For completeness, exit and return each accept an optional argument which is an integer (positive, negative or zero) which sets the return code as the remainder of the integer after division by 256. The current shell (or script or subshell*) is exited using exit and a function is exited using return.

Examples:

$ (exit -2); echo "$?"
254
$ foo () { return 2000; }; foo; echo $?
208

* This is true even for subshells which are created by pipes (except when both job control is disabled and lastpipe is enabled):

$ echo foo | while read -r s; do echo "$s"; exit 333; done; echo "$?"
77

Note that it's better to use break to leave loops, but its argument is for the number of levels of loops to break out of rather than a return code.

Job control is disabled using set +m, set +o monitor or shopt -u -o monitor. To enable lastpipe do shopt -s laspipe. If you do both of those, the exit in the preceding example will cause the while loop and the containing shell to both exit and the final echo there will not be performed.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested
while (($?))
do
    # do something until it returns 0    
done

Or you can emulate a do while loopemulate a do while loop:

while 
    # do some stuff
    # do some more stuff
    # do something until it returns 0    
do
    continue  # just let the body of the while be a no-op
done

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested
while (($?))
do
    # do something until it returns 0    
done

Or you can emulate a do while loop:

while 
    # do some stuff
    # do some more stuff
    # do something until it returns 0    
do
    continue  # just let the body of the while be a no-op
done

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested
while (($?))
do
    # do something until it returns 0    
done

Or you can emulate a do while loop:

while 
    # do some stuff
    # do some more stuff
    # do something until it returns 0    
do
    continue  # just let the body of the while be a no-op
done

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

additional information
Source Link
Dennis Williamson
  • 356k
  • 93
  • 379
  • 442

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested
while (($?))
do
    # do something until it returns 0    
done

Or you can emulate a do while loop:

while 
    # do some stuff
    # do some more stuff
    # do something until it returns 0    
do
    continue  # just let the body of the while be a no-op
done

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?"
42

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested
while (($?))
do
    # do something until it returns 0    
done

Or you can emulate a do while loop:

while 
    # do some stuff
    # do some more stuff
    # do something until it returns 0    
do
    continue  # just let the body of the while be a no-op
done

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

Source Link
Dennis Williamson
  • 356k
  • 93
  • 379
  • 442
Loading