0

Can I execute a grep in my script below?

echo 5
echo 4
result='çat output.txt | grep flag'
echo $result

The scipt gets used like ./script | ./program > output.txt

The script is used as input for the program, and the output of the program gets put into output, which I want to be able to grep for instantly. At the moment, it seems to finish without doing the grep command

Ben
  • 129
  • 9
  • `ç` should be `c` – Barmar Sep 21 '20 at 21:59
  • This doesn't make sense. You can't grep the output before you write to the output. – Barmar Sep 21 '20 at 22:00
  • @Barmar is there a way to read terminal output then? The program is set up so that only numbers are allowed as input, so the 4 is actually just exiting the program. This is why I have tried to put the contents in output.txt – Ben Sep 21 '20 at 22:04
  • If you're trying to feed those as input to another command, you should pipe them: `{ echo 5; echo 4; } | ./program` – Barmar Sep 21 '20 at 22:33
  • But if you're trying to send input to a program and also process its output, the tool to use for that is `expect`. – Barmar Sep 21 '20 at 22:35
  • @Barmar adding {echo 5; echo 4} throws an error- I dont think it is passing any of those as inputs to the program – Ben Sep 21 '20 at 23:17
  • You forgot the `;` after `echo 4` – Barmar Sep 22 '20 at 04:00

1 Answers1

0

I have the impression you are using single quotes while it should be backtics. Luckily there is something easier to use

result=$(cat output.txt | grep "flag")

The $(some_command) is used for getting the results of some_command.

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • This is in the duplicate question. Although it seems he has other problems, since he wants to grep from the same file that he's writing. – Barmar Sep 21 '20 at 22:34