5

Hopefully someone must know this one and I can answer my other question. Launchagent attempts to load my script too quickly and thus it fails - I've set its nice to 20 and everything else to the end but still, when creating accounts its too quick.

The script invokes cliclick which fails if the system is not properly logged in as an unknown command. So, my idea is this.

At the start of the script run cliclick -h which displays the help menu. If it fails on command unknown, keep repeating until it succeeds at which point carry on with the rest of the script.

Its a dirty method of making sure the script doesn't run until the user is at desktop and I can't think of a better one, but I also don't know how to implement it - so bash scripting gurus, how can I loop cliclick -h until its successful then continue with the rest of the script?

3 Answers 3

5

You can also use until:

until cliclick -h; do sleep 2; done ; say Done
4

I suspect there are better ways to do this, but the actual answer to your actual question is simple.

while ! cliclick -h; do
    sleep 1 # or more, or less
done

This assumes that cliclick -h correctly returns a zero (success) exit code. Some commands will return 1 when invoked with -h to distinguish from properly successful operation where some actual action was performed. Then maybe try the somewhat more fugly

while true; do
    cliclick -h || test $? -eq 1 && break
    sleep 1 # or more, or less
done
2
  • Well - I actually ended up using "until" the problem is, this doens't guarantee the script can run and more importantly, it stops cliclick-h working later on!
    – realdannys
    Commented Mar 24, 2015 at 22:12
  • I have a new idea though - seen as I don't want the script to run until the desktop is available, OS X creates a cache of the desktop image file...I could check for the existence of the file, if its not there, keep checking, if it there, run the rest of the script. Not sure exactly how though
    – realdannys
    Commented Mar 24, 2015 at 22:13
0

Try out this tool: https://github.com/kadwanev/retry

retry -t 1000 -e cliclick -h

check exit code to see if it succeeded at all after all retries...

1
  • 1
    Hey nkadwa going to make this as correct cos its a useful tool, I did try it too as well as just using "until". Unfortunately it wasn't this causing my problem and i'm still stuck on my script but nevermind!
    – realdannys
    Commented Mar 27, 2015 at 11:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.