0

I have a process for which I would like to ignore a certain class of errors for. Unfortunately, there aren't distinguishable return codes for these particular errors, but their are distinguishable error messages. In short, when running:

npm run build

The text 'npm ERR! missing script: build' will show up in stderr if a "build" script is missing. However, in this particular case, I don't want to consider that an error, i.e. I want to return 0 as teh return code.

Some thoughts on what the solution may entail:

  1. Redirect stderr to pipe to grep for 'npm ERR! missing script: build'
  2. Leave stdout alone
  3. If grep finds the text, it will return 0, which is good, because this is a "success" to me anyway. In this case, I just want stderr to be ignored, however as a bonus, it might be nice to write out a "warning" to stdout
  4. If grep doesn't find the text, then I want the text to be written out to stderr and I want the "original" error return code returned.

Update 1: So far, I tried the following, somewhat naive, approach:

#!/bin/bash

npm run build 2> temp.file
grep 'npm ERR! missing script: build' temp.file
RC=$?

if [[ RC -eq 0 ]]; then
  echo 'WARNING: No "build" script found'
fi

..then I ran the following test cases:


Scenario 1: Valid build script: "build": "echo 'placeholder build script'"

Output: $ ./build.sh

consumer-app-cod@1.0.0-alpha build C:\projects\epo-consumer-app\app echo 'placeholder build script'

'placeholder build script'

Expected: Looks good


Scenario 2: Invalid build script: "build": "invalid-bin"

$ ./build.sh

consumer-app-cod@1.0.0-alpha build C:\projects\epo-consumer-app\app invalid-bin

Expected: something like: $ invalid-bin bash: invalid-bin: command not found


Scenario 3: Missing build script

$ ./build.sh npm ERR! missing script: build WARNING: No "build" script found

Expected: Looks good


A few things:

  1. The second test case failed like it should have, but it didn't print stderr (because I'm not sure how to get stderr back to stdout after I already redirected it to the temp file).
  2. The temp file doesn't seem ideal and it seems like maybe there should be a way just to redirect it through pipes without the intermediate file, but I'm not even sure where to start with that
sparty02
  • 504
  • 1
  • 4
  • 13
  • What have you tried? Most of us here are happy to help you improve your craft, but are less happy acting as short order unpaid programming staff. Show us your work so far in an [MCVE](http://stackoverflow.com/help/mcve), the result you were expecting and the results you got, and we'll help you figure it out. – ghoti Jan 22 '18 at 22:13
  • @ghoti yeah, my bad, I should have included some more background before posting it at first. I've updated the original description. – sparty02 Jan 22 '18 at 22:42
  • 2
    `retval=0; exec 3>&1; npm_stderr=$(npm run build 2>&1 1>&3) || retval=$?; exec 3>&-; if (( retval != 0 )) && ! [[ $npm_stderr = *"Ignore this specific error"* ]]; then printf '%s\n' "$npm_stderr" >&2; exit "$retval"; fi` -- replace `"Ignore this specific error"` with whatever string you prefer. – Charles Duffy Jan 22 '18 at 22:52

0 Answers0