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:
- Redirect stderr to pipe to grep for 'npm ERR! missing script: build'
- Leave stdout alone
- 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
- 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:
- 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).
- 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