13 comments

[ 3.0 ms ] story [ 47.6 ms ] thread
I've so far only tested this on OS X, and have mostly used it with the `-t` mode to have it notify me (via "say") when ssh succeeds on a CloudFormation spawned AWS instance.
I went to go add a Homebrew formula for the name 'when', but it seems to be taken by this command-line calendar app: http://www.lightandmatter.com/when/when.html
I struggled with a name. This morning in the shower, I liked "trip" but, I'm not sure I'll change it--certainly not because of homebrew.
What is the difference between this and 'while true; do cmd1 && cmd2; sleep 1; done'
It looks to me more like

  until cmd1 ; do sleep 1 ; done ; cmd2
Exactly. But, see the -t flag, which is the real reason I wrote it. -t is a non-trivial script (if it's even possible in shell).

It defaults to the until behavior because it's most common.

Could you give some more examples of how you use `-t` ?
watch -n 1 foo && echo "why does this exist?"
Yes. See the -t flag. That's the reason I wrote it.
What's the difference between this and the bash '&&' operator? Except for the timebomb flag, I don't really see any difference. Please correct me if I got this wrong.

The '&&' operator, as in which ruby && echo 'found it!', triggers the second command, if the exit code of the first one is 0.

Also good to note, the '||' operator triggers the second command only if the exit code of the first one is different than 0. As in which ruby123456 || echo 'could not find it!'.

EDIT: typo.

It looks like if the first parameter fails, then it is re-run until it succeeds.
This could be quite handy as need to do this sort of thing surprisingly often and usually resort to a shell script one-liner like this:

    while true; do echo "hello world"; sleep 3; done
While that above code is pretty trivial to type, having a utility to this would be a great time saver for the lazy sysadmin :)

edit: someone else just posted about watch[1], never knew that existed before today. You learn something new every day :D

[1] https://news.ycombinator.com/item?id=6888640