-1

I have a two versions of a program, say : normalProgram and bruteProgram.

I have an input generator for both of these : programGenerator

Now I want to do this:

  • Put the output of ./programGenerator into input.txt : ./programGenerator > input.txt
  • Redirect input.txt as the input of normalProgram : cat input.txt | ./normalProgram
    • Put the output of ./normalProgram into op1.txt : (cat input.txt | ./normalProgram) > op1.txt
  • Similar thing for ./bruteProgram :
    • (cat input.txt | ./bruteProgram) > op2.txt
  • Now I want to compare op1.txt and op2.txt : diff op1.txt op2.txt

The whole command will look like this:

./programGenerator > input.txt &&
(cat input.txt | ./normalProgram) > op1.txt &&
(cat input.txt | ./bruteProgram) > op2.txt &&
diff op1.txt op2.txt

So this is a one time command.

I want to run this until diff op1.txt op2.txt gives a non-empty response.

I tried putting diff op1.txt op2.txt as the condition for until (or while, just to check whether the conditional is correct)

But this didn't work, and I got an error that, that is not a correct conditional.

Example:

while [diff normalOp.txt bruteOp.txt]
do
echo "hello"
done

This is giving me the error:

zsh: bad pattern: [diff
Mooncrater
  • 4,146
  • 4
  • 33
  • 62
  • 1
    Possible duplicate of ["Bash conditional based on exit code of command"](https://stackoverflow.com/questions/49849957/bash-conditional-based-on-exit-code-of-command/49850110). Also, if you did need to use square brackets, [you'd need spaces around them](https://stackoverflow.com/questions/19733437/getting-command-not-found-error-while-comparing-two-strings-in-bash). Finally, `(cat input.txt | ./normalProgram) > op1.txt` can be simplified to `./normalProgram < input.txt > op1.txt` (which is also more efficient). – Gordon Davisson Jan 08 '23 at 06:18

1 Answers1

0

I just had to remove the square brackets from the condition:

while diff normalOp.txt bruteOp.txt  
do
echo "hello"
done

And this started working.


The exact bash command in this case would be:

while diff op1.txt op2.txt
while> do
while> ./programGenerator > input.txt   
while> (cat input.txt | ./normalProgram) > op1.txt
while> (cat input.txt | ./bruteProgram) > op2.txt
while> done
Mooncrater
  • 4,146
  • 4
  • 33
  • 62
  • 1
    The syntax error was one problem, but your current logic is "while diff produces some output, do a thing." What you have stated in the question would be "while diff produces no output, do a thing." [This is for Bash, but should be similar in Zsh](https://stackoverflow.com/questions/3611846/bash-using-the-result-of-a-diff-in-a-if-statement), though you could also make it simpler using `until` instead of `while`. – Zac Anger Jan 08 '23 at 05:39