holman/spark — Issue #37: Requires trailing newline?
Author: gwern
Created: 2011-11-16 01:01:14+00:00
State: open

This is a tricky issue. I was going through the wiki page of examples and the readme, to remove all the `tr` munging now that we do '[:space:]' inside `spark` itself, when I discovered they weren't working! A stdin problem? But when I took the examples' output and manually echoed them in, `spark` worked! Huh?

$ echo -n 1,2, | spark && echo true
$

vs

$ echo 1,2, | spark && echo true
▁▂
true

I messed around and found that `echo -n` did cause the failure. After some more digging and use of `set -x` and `tee`, I found that `spark` was bombing out with an error at line 148, with `read`. If you do `help read` you find it demands a newline at the end of the input, or at least that its caller specify an end-of-input delimiter. We can't really do that because the last character will usually be a number we want! So I went to #bash and asked for help. It turns out that `set -e` is the problem - `read` is populating the array as we want it to, but then it reaches the end of input, returns a bad exit code, and per the arcane ad hoc rules governing it (http://mywiki.wooledge.org/BashFAQ/105) that kills the script. Or something like that. Bash example (note which one prints out):

$ echo -n 3,4, | { read var && echo true1; echo "$var"; } && echo true2
3,4,
true2

Removing `set -e` fixes the problem. The FAQ says 'GreyCat's personal recommendation is simple: don't use set -e. Add your own error checking instead.' so hopefully that wouldn't be too big an issue.