0

I'm trying to compare the output of my fizzbuzz.js program:

for (let i = 1; i <= 100; i++) {
  let out = "";
  if (i % 3 == 0) out += "Fizz";
  if (i % 5 == 0) out += "Buzz";
  if (i % 3 && i % 5) out = i;
  console.log(out);
}

with the contents of a expected-output.txt file which contains:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
[...]

I can't get process substitution to work:

diff -u expected-output.txt <(node fizzbuzz.js)

seems to hang indefinitely. What could be causing this?

My environment is macOS Mojave, bash 3.2.57, node v12.5.0.

EDIT: this seems to be a macOS issue. Everything works fine on a Ubuntu 18.04 machine with newer bash 4.4.19

Chef Tony
  • 455
  • Thanks for the negative vote. I thought you know how to call diff and it's just about the process substitution, as the title suggests. Make your questions clearer. That will increase your chances of getting an appropriate answer. I'll not investigate further here. – jottbe Jul 14 '19 at 18:35
  • @jottbe I didn't give you a negative vote – Chef Tony Jul 14 '19 at 18:38
  • 2
    @jottbe : I did give the negative note, because it was not a correct answer and not even a correct comment. – FedKad Jul 14 '19 at 18:42
  • Does the command diff -u expected-output.txt <(cat expected-output.txt) terminate correctly? – FedKad Jul 14 '19 at 18:44
  • @FedonKadifeli It does (exit code 0) – Chef Tony Jul 14 '19 at 18:45
  • As I've added in the post, this only seems to happen on macOS. Maybe it's the older bash version? – Chef Tony Jul 14 '19 at 18:49
  • 2
    Can you try node fizzbuzz.js | diff -u expected-output.txt -? – FedKad Jul 14 '19 at 18:50
  • @FedonKadifeli that works! Thanks! – Chef Tony Jul 14 '19 at 18:51
  • 1
    Probably you bumped to the "seek problem" explained in https://unix.stackexchange.com/questions/164107/why-does-bash-process-substitution-not-work-with-some-commands – FedKad Jul 14 '19 at 18:52
  • Because read var <(echo hello) didn't work as command substitution, but read var < <(echo hello) does, I thought that was the problem. And @ChefTony comments help some times. At least for those who are fair and have the courage! thanks – jottbe Jul 14 '19 at 19:04
  • 1
    @FedonKadifeli, nah, diff shouldn't need to seek. And if it did, it would probably give a clear error about it anyway. – ilkkachu Jul 14 '19 at 19:22
  • 2
    @jottbe, yeah, read reads from stdin, but diff expects a filename. The <(...) expands to a file name, so can be used as-is with diff. On the other hand, < <(...) redirects to stdin from that "file" – ilkkachu Jul 14 '19 at 19:23
  • Obviously, there is some difference between the cat command and the node command here. Since I have no access to a system similar to the OP's, I cannot do my own tests. – FedKad Jul 14 '19 at 19:29
  • 1
    will cat <(node fizzbuzz.js) hang too? also, what does ls -l <(true) say (I don't know about MacOS, but on some systems, bash will use named pipes for process substitutions). –  Jul 14 '19 at 21:49
  • @mosvy yes, cat <(node fizzbuzz.js) hangs too, it displays the output but doesn't exit until I ^C. ls -l <(true) returns prw-rw---- 0 user staff 0 Jul 15 06:29 /dev/fd/63 – Chef Tony Jul 15 '19 at 04:30

0 Answers0