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
6
  • Use $?. This will return the last command exit code. Commented Mar 27, 2018 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 Commented Mar 27, 2018 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. Commented Mar 27, 2018 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. Commented Mar 27, 2018 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. Commented Mar 27, 2018 at 20:22

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

I'd prefer if ./node<etc> | 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.
@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.
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

1 Comment

Your condition will always be true; you need spaces around =.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.