Looping on bash exit code

Just want to start with the simple fact that I'm a relative bash newbie and i'm probably doing it wrong.

Anyway, i'm working on a script and I have a program that doesn't always execute reliably due to reasons involving our network. Command works fine, network sometimes causes it to fail. So what i'm trying to do is to incorporate it's execution into a simple loop that forces it to rerun until it's successful. What I have so far is this ...

There are lots of problems here. Traditionally, 0 is used to indicate success and anything else a failure when a command returns.

Your biggest problem is your loop will never execute. Your second problem is that no matter what happens, the loop will only execute once, because you shut down the computer whenever the errorcode is zero.

I'm quite confused about what you're actually trying to accomplish, but if you just want to run a command many times, then:

The idea is that if the command executes successfully the loop ends and proceeds on with the rest of the script.

If the command fails, the machine reboots and the script executes on boot again ... and again ... and again until it does. I'm fairly sure == means not equal so i'm not sure where you get the idea that I shut the computer down if it works? btw shutdown -r on os x bash means restart

You're right it's a horrible solution. Unfortunately i'm dealing with flaky AFP shares that randomly either don't mount properly or don't unmount properly. (caused by JAMF's casper repeatedly mounting/dismounting the same share over and over ... urgh. not much I can do about that.)

A reboot is the best solution all round. Thanks guys for the pointers between string and numeric variables.

That's easy. Delete the launchd plist and the folder where all the scripts are in, then force "yet another reboot (tm)". Our current system is less than friendly and the 2nd line guys ... well some of them I wonder about, put it that way.

It's been educational, which is always a good thing. Thank you everyone!

You have to understand that the "[ $retval -ne 0 ]" expression is not built into the shell script language*. What actually happens is that the "test" program (man test), also known '[' (see /bin/[) is invoked with 4 command line arguments ( 'value-of-retval' '-ne' '0' ']' ). It will evaluate the given expression and return a value based on this evaluation. "if" only operates on return values, as does "while". Storing the return value in order to evaluate it with another program is kind of backwards.

</end of pedantry>

* The "test" command may actually be a builtin of a certain shell for performance reasons.