1

I have the following shell script:

#!/usr/bin/env sh
./node_modules/.bin/nightwatch --env chrome --tag=enabled
exit 0

The nightwatch command always returns the exit code 1, doen’t matter if the nightwatch tests will fail or pass. So, I want to check if the console output of this command contains a specific string (maybe failed) to handle on it and to return a right exit code with the shell script.

The only requirement I have is, that the nightwatch command output is visible on console because we will need it because of debugging reasons.

I want to do something like this (pseudo code):

#!/usr/bin/env sh
./node_modules/.bin/nightwatch --env chrome --tag=enabled
if lastOutput.contains("failed"); then
  exit 1
else
  exit 0
fi
GRme
  • 2,707
  • 5
  • 26
  • 49
  • Use $?. This will return the last command exit code. – Joao Vitorino Mar 27 '18 at 19:56
  • You can use command substitution. `lastOutput=$(./node_modules/.bin/nightwatch --env chrome --tag=enabled)` then examine `lastOutput` as `[[ $lastOutput = *failed* ]] && exit 1 || exit 0` – anubhava Mar 27 '18 at 19:56
  • But nightwatch always returns exit code 1, doesn‘t matter if the nightwatch tests will fail or not. So, I have to parse the nightwatch output to decide which exit code I have to return. – GRme Mar 27 '18 at 19:58
  • 1
    @Martin I'd spend some time figuring out *why* it always exits 1; that's not (or should not be) normal. – chepner Mar 27 '18 at 20:20
  • that‘s right. that is a nghtwatch or a selenium bug, I know. But at first I have to find a workaround for me. I‘ve also created an bug issue for the nightwatch team. – GRme Mar 27 '18 at 20:22
  • You can do this with https://stackoverflow.com/questions/16931244/checking-if-output-of-a-command-contains-a-certain-string-in-a-shell-script plus https://stackoverflow.com/questions/12451278/bash-capture-stdout-to-a-variable-but-still-display-it-in-the-console – that other guy Mar 27 '18 at 23:14

2 Answers2

2

Since you are running this on the POSIX bourne shell sh, you could do a comparison of the command output with the [ operator or the case construct

case "$(./node_modules/.bin/nightwatch --env chrome --tag=enabled)" in
  *failed*)  exit 1;;
   *) exit 0 ;;
esac

or use the return code of grep and asking it to be silent with the -q flag set

if ./node_modules/.bin/nightwatch --env chrome --tag=enabled | grep -q failed; then
    exit 1 
else
    exit 0
fi
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    I'd prefer `if ./node | grep -q 'failed'; then exit 1; else exit 0; fi` instead of using `$?` - on less statement and no need to take care of `$?`. I'd post an answer, but it's really practically the same as yours, hence just commenting here. – Benjamin W. Mar 27 '18 at 20:35
  • @Inian : Since the OP says *console output* and not *standard output*, we have to care for the case that the error message **failed** is printed to standard error (a not unlikely scenario), so you should add a `2>&1` to your solution. – user1934428 Mar 28 '18 at 06:32
0

Plenty of more efficient ways to go about this, but according to your pseudocode:

#!/usr/bin/env sh
lastOutput=$(./node_modules/.bin/nightwatch --env chrome --tag=enabled)
if [[ $lastOutput = *"failed"* ]]; then
  exit 1
else
  exit 0
fi
beau
  • 16
  • 2