Checking if output of a command contains a certain string in a shell script

bash check if output of command is empty
bash check if command output equals string
bash if
check output of command shell script
how to check if a string starts with a substring in shell script
bash if string contains
bash string contains regex
bash check if string does not contain substring

I'm writing a shell script, and I'm trying to check if the output of a command contains a certain string. I'm thinking I probably have to use grep, but I'm not sure how. Does anyone know?

Test the return value of grep:

./somecommand | grep 'string' &> /dev/null
if [ $? == 0 ]; then
   echo "matched"
fi

which is done idiomatically like so:

if ./somecommand | grep -q 'string'; then
   echo "matched"
fi

and also:

./somecommand | grep -q 'string' && echo 'matched'

Check if the particular string is present in a output of shell script , If the output will always contain only 2 lines - awk solution to check by multiple fields: awk 'NR==2 { printf "%s%s\n"� 1. If you want a ksh only method that is as fast as "test", you can do something like: contains() # haystack needle { haystack=${1/$2/} if [ ${#haystack} -ne ${#1} ] ; then return 1 fi return 0 } It works by deleting the needle in the haystack and then comparing the string length of old and new haystacks. share.

Testing $? is an anti-pattern

if ./somecommand | grep -q 'string'; then
  echo "matched"
fi

How to Check if a String Contains a Substring in Bash, Assuming you don't need the program output for anything else, I would do it like this: for var in ./* do while ! python2.7 submit.py "$var" | grep -qFe traceback do� The grep command can also be used to find strings in another string. In the following example, we are passing the string $STR as an input to grep and checking if the string $SUB is found within the input string. The command will return true or false as appropriate.

Another option is to check for regular expression match on the command output.

For example:

[[ "$(./somecommand)" =~ "sub string" ]] && echo "Output includes 'sub string'"

How to check in shell script if output of command contains a specific , How can I check if a string contains a Substring in Bash scripting? For example find out if a word called tips exists in $var="Here are some tips for you" . Using regex operator; � awk syntax; � grep command; � Conclusion� Also, grep 'string' &>/dev/null is both non-POSIX compliant and much slower to execute (if string appears early in a long output stream) than grep -q string. [The caveat there is if you want to be sure somecommand keeps running even after emitting string, in which case using grep -q-- by closing its stdin and exiting after the first instance of string is seen -- can be counterproductive].

A clean if/else conditional shell script:

if ./somecommand | grep -q 'some_string'; then
  echo "exists"
else
  echo "doesn't exist"
fi

Bash Find Out IF a Variable Contains a Substring, Checking if output of a command contains a certain string in a shell , Test the Check if the particular string is present in a output of shell script , If the output will � We can use the powershell’s like operator with wildcard character to check if a string contains a word or another string with case-sensitive and case-insensitive. Note : You can not use the comparison operator contains to check the contains string, because it’s designed to tell you if a collection of objects includes (‘contains’) a particular object.

How to check if the output of a command contains a string and then , I need to check the output of apachectl configtest in a bash script and restart if everything Checking if output of a command contains a certain string in a shell � Sometimes, we need to check if the pattern presents in a file and take some actions depending on the result. It can be done with the help of ‘exit status codes’. Each Linux command returns a status when it terminates normally or abnormally You can use command exit status in the shell script to display […]

Bash: Check output of command, I'm writing a shell script, and I'm trying to check if the output of a command contains a certain string. I'm thinking I probably have to use grep, but I'm not sure how. 1) run a command that has some output. 2) check if the output contains some text 3) if yes - do something What I tryed to do is to save the output to a file and then save the data from file to variable: [command] > c:\temp.txt set /p RetVal= < c:\temp.txt del temp.txt But it doesn't work well. Anybody has some ideas how to do all these? Thanks

bash, and whenever it contains a certain string I want to run another command. Trick here, is that if statements operate on exit statuses of commands, and the exit or not the command succeeded (i.e. grep found desired string in the output) or not if non-zero. Or bash 's extended test [[ for pattern matching: You can always find out if a string/word/variable contains another string/word in Linux or Unix shell scripting. For example find out if a word called tips exists in $var="Here are some tips for you" .

Comments
  • Does the command need to keep running after generating the output string you're looking for, or can it be immediately closed at that time? (Your two answers differ in terms of their semantics in this respect).
  • This code doesn't work with all POSIX shells: The POSIX standard only requires = to be a comparison operator, not ==; see pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
  • Also, grep 'string' &>/dev/null is both non-POSIX compliant and much slower to execute (if string appears early in a long output stream) than grep -q string. [The caveat there is if you want to be sure somecommand keeps running even after emitting string, in which case using grep -q -- by closing its stdin and exiting after the first instance of string is seen -- can be counterproductive]. (Re: "non-POSIX-compliant", &> is an extension -- see pubs.opengroup.org/onlinepubs/009695399/utilities/… describing the POSIX-mandated redirection support).
  • Would help if it was explained why that works/what each parameter does, to encourage full understanding of the syntax
  • If by any chance you only want to test a fixed string, add F and x options: grep -Fxq F stands for fixed (not interpreted) and x for the whole line
  • Why testing of $? is anti-pattern?
  • @VitalyZdanevich I assume because it's not robust against concurrency.
  • @VitalyZdanevich, for one, testing $? doesn't set the preceding commands as "checked" for purposes of set -e or the ERR trap, so your program can exit in cases where you want it to simply return down the intentionally-false path later. For another, $? is volatile global state -- it's easy to throw away its value by accident. For example, if you add a line of logging like echo "Exit status is $?", the new value in $? becomes the exit status of echo.