0

Im modifying an existing bash script and having some trouble getting the while loop behaving correctly. This is the original code

while ! /usr/bin/executable1
do
    # executable1 returned an error. So sleep for some time try again
    sleep 2
done

I would like to change this to the following

while ! /usr/bin/executable1 && ! $(myfunc)
do
    # executable1 and myfunc both were unsuccessful. So sleep for some time
    sleep 2
done

executable1 returns 0 on success and 1 on failure. I understand that "true" in bash evaluates to 0 so thats why the original script would keep looping till executable returned success

Accordingly myfunc is coded like this

myfunc ()
{
    # Check if file exists. If exits, return 0, If not, return 1
    if [ -e someFile ]; then
        return 0
    fi

    return 1 
 }

I notice that the my new while loop does not seem to call executable1. It always calls myfunc() and then exits out of the loop immediately. What am I doing wrong?

I tried various ways of coding the while loop (with (( )), [ ], [[ ]] etc), but nothing seems to fix it

3
  • It's not clear why you think executable1 is not run at all; it must be run in order to decide (due to &&) whether or not to run myfunc. Commented Mar 21, 2016 at 0:24
  • executable 1 when run by itself takes close to 8-10 seconds to complete. (it actually runs "ntpdate") So Im pretty sure it doesnt get called because the while loop returns almost immediately. Commented Mar 22, 2016 at 2:34
  • I think I may have figured out the problem in my code. In bash it turns out that boolean true evaluates to 0 and boolean false evaluates to 1. So while (( 0 )) is actually an infinite while loop. What I want to do is enter the while loop when the file is not found. So I should return 1 (false) when file is not found and 0 (true) when file is found. Once I made this change, it all worked Commented Mar 22, 2016 at 2:37

1 Answer 1

1

You don't need $(...) to call a function, just to capture its standard output. You simply want

while ! /usr/bin/executable1 && ! myfunc
do
    sleep 2
done

Note that myfunc can also be more simply written

myfunc () {
    [ -e someFile ]
}

or even (in bash)

myfunc () [[ -e someFile ]]

Either way, it's almost not worth defining myfunc separately; just use

while ! /usr/bin/executable1 && ! [[ -e someFile ]]
do
    sleep 2
done

It might also be simpler to use an until loop:

until /usr/bin/executable1 || [[ -e someFile ]]; do
    sleep 2
done
Sign up to request clarification or add additional context in comments.

1 Comment

Ive provided just the essential details of my code in this question. The myfunc() is reality does a lot many things, but the main thing is that it returns 1 or 0 to the caller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.