0

I have some shell process, i need to catch errors that comes with it by string from specific line. So, i'm doing this:

OUTPUT="$(npm run generate_post)"
echo ${OUTPUT} | grep "_currentUrl" * > error.log

but i don't have any results, can anyone help?

Lukas
  • 7,384
  • 20
  • 72
  • 127
  • Why do you have `*` after `grep` while reading input from a pipe? It should just be `echo "${OUTPUT}" | grep "_currentUrl" > error.log` or just `npm run generate_post | grep "_currentUrl" > error.log` – anubhava Jul 31 '17 at 15:54

2 Answers2

1

I'm gonna guess that your npm run generate_post is outputting errors on STDERR, but you're capture STDOUT. Try this:

OUTPUT="$(npm run generate_post 2>&1)"
echo ${OUTPUT} | grep "_currentUrl" * > error.log

Note that the above would capture both STDERR and STDOUT. More information in this answer.

Kenan Banks
  • 207,056
  • 34
  • 155
  • 173
  • this is what i have in my error.log file => `generate_post.sh:echo ${OUTPUT} | grep "_currentUrl" * > error.log` :( – Lukas Jul 31 '17 at 16:02
1

First of all, your statement doesn't make sense:

 echo ${OUTPUT} | grep "_currentUrl" *

The asterisk expands to the files in the working directory, and grep is searching the pattern there, ignoring what comes from stdin. I have no idea what you intended with *.

Then, you didn't specify whether you need the full output of npm later on. Assuming that you don't, you can write

npm run generate_post | grep _currentUrl

Of course it might be wise to follow the advice by Triptych to capture stderr too, but this is a different story.

If you do need the full output of npm, consider putting it into a file instead of a variable - of course this depends on how you are going to use it later, so this is just one of several options:

npm run generate_post | tee npm_output.txt | grep _currentUrl
user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Hey, it's working fine, but how can i print the line that contains this phrase "_currentUrl", right now i have all data from npm ... THX – Lukas Aug 01 '17 at 07:26
  • ???? The grep command ensures that the STDOUT only lines containing the string _currentUrl. If you see additional information, this is most likely not STDOUT, but STDERR, as has been repeatedly pointed out in this thread. – user1934428 Aug 01 '17 at 12:54