0

With the following:

curl URL | grep -i "KEYWORD" && "keyword found" || echo "keyword not found" >> result.txt

How can I get the results into the file? Seems this does not work with or without the file created.

Objective:

Search for keyword in curl result, if found append to file 'found' else append 'not found'.

Edit:

I figured it out:

curl URL | grep -i "keyword" && echo "found $(date)" >> result || echo "not found $(date)" >> result
iCodeLikeImDrunk
  • 17,085
  • 35
  • 108
  • 169
  • `curl URL | tee result.txt | grep ....` – KamilCuk Jun 17 '19 at 17:10
  • 1
    What is *"does not work"* mean? Also see [How can I redirect and append both stdout and stderr to a file with Bash?](https://stackoverflow.com/q/876239/608639), [Redirect stderr and stdout in Bash](https://stackoverflow.com/q/637827/608639) and [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – jww Jun 17 '19 at 17:29
  • when i run my command, it will not create the file(if it wasnt created) and append to the file. – iCodeLikeImDrunk Jun 17 '19 at 17:44

1 Answers1

1

You can redirect the output for a whole if command:

if curl URL | grep -i 'KEYWORD'; then echo "keyword found"; else echo "keyword not found"; fi >> result.txt

If it's not a single command, you can enclose the group in { ;}:

{ command1; command2; command3; } >> result.txt

Note that cmd1 && cmd2 || cmd3 can produce unexpected results. In particular, if cmd1 succeeds but cmd2 fails, cmd3 will also run. if / fi is less error prone.

melpomene
  • 84,125
  • 8
  • 85
  • 148