Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Check If Output Contains String in Bash

Using grep Command

Use grep with the -q option to check if output contains String in Bash.

#!/bin/bash
output=$(ls -l)
search_string=".csv"  # String to search for in the output
if echo "$output" | grep -q "$search_string"; then
    echo "The output contains the search string."
else
    echo "The output does not contain the search string."
fi
The output contains the search string.

In this example, the ls command is used to get the list of directories and files in the current working directory. Here, the -1 parameter is used with the ls command to get detailed information about the list and directories in a long format. The output of this command is stored in the $output variable using the command substitution $(...). Then, the string we want to search is stored in a $search_string variable as .csv.

After that, the conditional statement is used to check whether the output contains the Search String. In the if block, the output of the echo "$output" command is passed to the grep command via the pipe | operator. The -q operator tells the grep to be quiet and not display output.

The grep command searched for the search string .csv in the input string received from the previous command. If the grep finds the search string in the $output variable, it sends the success status zero to show that the search string is found, and the if block will execute, displaying the message "The output contains the search string." on the screen.

But if grep does not find any match, it returns the non-zero status to show that match is not found, and the else block will be executed. In the above case, the search string .csv is found in the $output variable and the output is returned as The output contains the search string.

Now, let’s print the list of files and directories in the current working directory to verify the result of the above bash script.

#!/bin/bash
output=$(ls -l)
echo "$output"
total 7
-rw-r--r-- 1 DELL 197610 99 Jun 12 12:21 my.csv
drwxr-xr-x 1 DELL 197610  0 Jul  9 13:00 review it by self
-rw-r--r-- 1 DELL 197610 84 Jun 12 11:38 sortedFile.csv
-rwxr-xr-x 1 DELL 197610 44 Jul 10 12:31 test.sh

The list of files and directories in the current working directory is displayed in the given example. We can observe that the string .csv is available in the given output. So, it is verified. Have a look at another example to get a better understanding.

#!/bin/bash
output=$(curl -s https://example.com) 
search_string="illustrative examples"  
if echo "$output" | grep -q "$search_string"; then
    echo "The output contains the search string."
else
    echo "The output does not contain the search string."
fi
The output contains the search string.

In this bash script, the curl command fetches the website’s content, and the output is stored in the $output variable. The search string illustrative examples is stored in the $search_string variable; after that, a conditional statement is used to search the required string in the output variable using the grep command, as in the above example.

Using the awk Command

Use the awk and the string comparison to check if output contains String in Bash.

#!/bin/bash
output=$(curl -s https://example.com)
search_string="happy"  # String to search for in the output
if echo "$output" | awk -v search="$search_string" 'tolower($0) ~ tolower(search) { found=1; exit } END { exit !found }'; then
    echo "The output contains the search string."
else
    echo "The output does not contain the search string."
fi
The output does not contain the search string.

In this example, the awk is used to search the search string happy in the $output variable. The output of echo "$output" is passed to awk using the | pipe operator for processing. Then, the -v flag sets the value of the search string in the awk search variable. The tolower() method converted the current line($0) and the search string in lowercase. Here, the ~ operator searches if the search_string is found in the lowercase version of the current line.

If the match is found, the found variable will be set to 1 and exit the awk. The END { exit !found }' block is executed at the end of processing all the lines. If the found variable is set as 1, representing a match was found, in this case, the awk exits with exit status 0. On the other hand, it exits with a non-zero status (showing no match found).

After that, the exit code of awk is evaluated using the if condition in the bash script. If the exit status is 0 the if block executes with the message "The output contains the search string." If the status code is non-zero, the condition evaluates to false, and the else block executes with the message The output does not contain the search string..

The output of the above example shows that the search string happy was not found in the output.

Using Regular Expression

Use the regular expression to check if output contains String in Bash.

output="The quick brown fox jumps over the lazy dog."
pattern="quick.*jumps"
if [[ $output =~ $pattern]]; then
    echo "The output matches the pattern."
else
    echo "The output does not match the pattern."
fi
The output matches the pattern.

In this bash script, let’s assume the output is The quick brown fox jumps over the lazy dog. and the search string is stored in the $pattern variable as a regular expression "quick.*jumps"

In Bash, the `[[ ... =~ ... ]] construct is used for regular expression matching. Here, the =~ operator matches the left-hand side $output with the regular expression $pattern on the right-hand side.

If the match is found, the if block will execute. The [[ $output =~ $pattern]] condition evaluates as true and the message "The output matches the pattern." is printed. Otherwise, the condition evaluates as false, and the else block will execute with the message "The output does not match the pattern.".

That’s all about how to check if Output contains String in Bash.



This post first appeared on How To Learn Java Programming, please read the originial post: here

Share the post

Check If Output Contains String in Bash

×

Subscribe to How To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×