Reputation: 23
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.
Upvotes: 2
Views: 28
Reputation: 140266
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>
Upvotes: 1