2

I am trying to check to see if a string exists inside a variable inside an if statement.

if  [ -n $(echo "$RUN" | grep "Done running command.") ]; then

I know this can be done by creating another variable then checking if that is empty but I am trying to see if it can be done in a more streamlined way.

CHK=$(echo "$RUN" | grep "Done running command.")
if  [ -n "$CHK" ]; then

Any incite on this would be helpful.

1 Answer 1

1

You can use regex matches, which will save the spawning of a grep command, for a better performance and bash integration:

if [[ "$RUN" =~ "Done running command." ]] ; then echo OK ; fi

(note that if the dot is withing the quotes, it acts as a normal dot, not a wildcard character: that's for the best for our example)

test:

RUN="ok, Done running command. Bye"
OK

RUN="not running command. Bye"
<nothing printed>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.