Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
Shell scripts are a useful feature in the Linux operating system. We can use shell scripting to carry out various operations, such as installing and customizing various applications, fixing issues, and more.
Conditional expressions are essential in shell scripting because they allow us to execute statements based on a specific condition. We will especially focus on the if-else statement for this article.
On the other hand, the grep command in Linux helps us search for matching patterns in a given file.
In this tutorial, we’ll discuss the process of including the grep command in an if-else statement in Bash. But before we do that, we’ll use grep in a simple if statement to get an idea of how it works.
Firstly, let’s create a shell script (file.sh) that contains the if statement:
#!/bin/bash
num=1
if [ $num -eq 1 ]
then
echo "The value is 1."
fi
Above, we’re checking if the value of the num variable is equal to (-eq) 1. If it is, we’ll print the given statement.
Now, let’s make it executable:
$ chmod +x file.sh
Finally, let’s run it to get the output:
$ ./file.sh
The value is 1.
As we can see, the output is showing us what we expected.
So, let’s now integrate the grep command into the if statement to perform an advanced operation:
#!/bin/bash
if [ $(grep -c "Baeldung" /projects) -eq 1 ]
then
echo "We have found Baeldung."
fi
Above, we’ve used grep to find the string Baeldung in the projects file. The -c option tells grep to count its total number of occurrences. If the count is equal to 1, we’ll print the given statement.
The output will be:
$ ./file.sh
We have found Baeldung.
As we can see, the given condition is true; thus, the output prints the given statement.
If we want the search to be case-insensitive, we can append the -i option, short for –ignore-case, to the grep -c command:
#!/bin/bash
if [ $(grep -ic "Baeldung" /projects) -eq 1 ]
then
echo "We have found Baeldung."
fi
Here’s the output:
$ ./file.sh
We have found Baeldung.
So, let’s move on to the next section, where we’ll combine the grep command with the if-else statement.
Let’s have a look at the below shell script:
#!/bin/bash
if [ $(grep -ic "Baeldung" /projects) -eq 1 ]
then
echo "We have found Baeldung."
else
echo "We haven't found Baeldung."
fi
The logic is simple:
Let’s check the output:
$ ./file.sh
We haven't found Baeldung.
As we can see, the given condition is false, and hence, the output prints the statement under else.
So, now that we’ve seen how to use the grep command in an if-else statement in one of the simplest ways possible, we can take this a step further and combine grep with the else-if statement:
#!/bin/bash
if [ $(grep -ic "Baeldung" /projects) -eq 1 ]
then
echo "We have found Baeldung."
elif [ $(grep -ic "Baeldung" /projects) -gt 1 ]
then
echo "Baeldung is there multiple times."
else
echo "We haven't found Baeldung."
fi
Let’s review the logic:
Here’s the output:
$ ./file.sh
Baeldung is there multiple times.
The output indicates that the elif statement condition is true, and thus, the output prints the statement under elif.
Moreover, we can also use grep with the nested if statement:
#!/bin/bash
if [ $(grep -ic "Baeldung" /projects) -ge 1 ]
then
if [ $(grep -ic "Baeldung" /projects) -eq 1 ]
then
echo "We have found Baeldung."
elif [ $(grep -ic "Baeldung" /projects) -eq 2 ]
then
echo "Baeldung is mentioned 2 times."
else
echo "Baeldung is mentioned more than twice."
fi
Let’s understand the logic step-by-step:
Let’s check the output:
$ ./file.sh
We have found Baeldung.
As we can see, the nested if statement condition is satisfied, and hence, the output prints the statement under the nested if.
In this article, we’ve seen how to use grep in an if-else statement and potentially increase the usefulness of our shell scripts.
The grep command is undoubtedly a useful command for shell scripting, as it allows us to perform an additional range of operations.