Bash: Loop Until Command Exit Status Equals 0

Bash: Loop until command exit status equals 0

Keep it Simple

until nc -z 127.0.0.1 25565
do
echo ...
sleep 1
done

Just let the shell deal with the exit status implicitly

The shell can deal with the exit status (recorded in $?) in two ways, explicit, and implicit.

Explicit: status=$?, which allows for further processing.

Implicit:

For every statement, in your mind, add the word "succeeds" to the command, and then add
if, until or while constructs around them, until the phrase makes sense.

until nc succeeds; do ...; done


The -z option will stop nc from reading stdin, so there's no need for the < /dev/null redirect.

Bash loop until a certain command stops failing

In addition to the well-known while loop, POSIX provides an until loop that eliminates the need to negate the exit status of my_command.

# To demonstrate
my_command () { read number; return $number; }

until my_command; do
if [ $? -eq 5 ]; then
echo "Error was 5"
else
echo "Error was not 5"
fi
# potentially, other code follows...
done

How can you run a command in bash over and over until success?

until passwd
do
echo "Try again"
done

or

while ! passwd
do
echo "Try again"
done

How to get the exit status a loop in bash

The status of the loop is the status of the last command that executes. You can use break to break out of the loop, but if the break is successful, then the status of the loop will be 0. However, you can use a subshell and exit instead of breaking. In other words:

for i in foo bar; do echo $i; false; break; done; echo $?  # The loop succeeds
( for i in foo bar; do echo $i; false; exit; done ); echo $? # The loop fails

You could also put the loop in a function and return a value from it. eg:

in() { local c="$1"; shift; for i; do test "$i" = "$c" && return 0; done; return 1; }

How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0?

wait also (optionally) takes the PID of the process to wait for, and with $! you get the PID of the last command launched in the background.
Modify the loop to store the PID of each spawned sub-process into an array, and then loop again waiting on each PID.

# run processes and store pids in array
for i in $n_procs; do
./procs[${i}] &
pids[${i}]=$!
done

# wait for all pids
for pid in ${pids[*]}; do
wait $pid
done

How to break out of a loop in Bash?

It's not that different in bash.

workdone=0
while : ; do
...
if [ "$workdone" -ne 0 ]; then
break
fi
done

: is the no-op command; its exit status is always 0, so the loop runs until workdone is given a non-zero value.


There are many ways you could set and test the value of workdone in order to exit the loop; the one I show above should work in any POSIX-compatible shell.

Bash while loop, how to read input until a condition is false

Give space around brackets in test conditions

while [ ! ${finished} ]

&

if [ $input = "done" ]


Related Topics



Leave a reply



Submit