Hacker News new | ask | show | jobs
by chubot 3446 days ago
Shell and bash are actually excellent at this, but people don't like writing shell scripts. This is just process chaining. Take a look at DJB's or the more modern runit for init toolkits that compose.

https://cr.yp.to/daemontools.html

http://smarden.org/runit/

Here is a bash function that retries N times:

    retry() {
      local n=$1
      shift
      for i in $(seq $n); do
        "$@"
      done
    }

    retry 5 echo hi
    
Then you can compose with a timeout function, which already exists:

    timeout-demo() {
      timeout 0.1 $0 retry 5 echo hi
    }
You can pass the names of other functions as pre and post hooks as well.

https://www.gnu.org/software/coreutils/manual/html_node/time...

Shell has a very forth-like quality to it, and Forth is sort of like a backwards Lisp as well (postfix rather than prefix).

2 comments

Isn't it just repeats the command 5 times instead of retrying?

IMO Bash with it's multitude of annoying quoting and field splitting rules, many irrelevant features focusing on interactive use, and error handling as an afterthought is just wrong choice for writing robust systems. It's too easy to make mistakes. And it still works only in the simplest cases, until somebody evil deliberately pass you newline delimited string or something with patterns which expands in unexpected place, etc. Properly handling those cases will make your script ugly mess. Actually I find the mental burden when writing shell scripts is very akin to programming in C.

> Bash with it's multitude of annoying quoting and field splitting rules

So don't use the Bourne Again shell. After all and to start with, if you live in the Debian or Ubuntu worlds, your van Smoorenburg rc scripts have not been using the Bourne Again shell for about a decade.

There's no reason at all that run programs need be written in any shell script at all, let alone in the Bourne Shell script. Laurent Bercot publishes a tool named execline that takes the ideas of the old Thompson shell ("if" being an external command and so forth) to their logical conclusions, which is far better suited to what's being discussed here. One can also write run programs in Perl or Python, or write them as nosh scripts.

* http://blog.infinitenegativeutility.com/2015/2/celebrating-d...

If the command is a daemon, that's basically retrying. But if you want to check the exit code, that's easy to do inside retry().

I totally agree with your second paragraph, that is why I'm working on fixing shell :)

http://www.oilshell.org/blog/

This entry in particular is relevant to your concerns:

http://www.oilshell.org/blog/2016/11/06.html

I guess it's a bit like advanced perl5, you need to be taught the "right way" otherwise you pile up overlapping imperative code.
Thanks for the suggestion -- I expanded on this idea in a blog post:

http://www.oilshell.org/blog/2017/01/13.html

Feedback appreciated!

You forgot rtprio, idprio, chrt, and numactl; the s6, perp, daemontools-encore, and nosh toolsets; and execline.

* http://wiki.tcl.tk/15088

Then of course there are TCL and the Thompson shell.

* http://v6shell.org/