Shell Script To Check the String For a Particular Condition
Last Updated :
14 Dec, 2022
Here, we are going to write a shell script that will echo (print) a suitable message if the accepted string does not meet a particular condition. Suppose we have given some certain conditions then our task is to print the message if the accepted string doesn't match the condition.
Example 1:
If the accepted string's length is smaller than 10, we will display a message saying "String is too short".
Input: "abcdef"
Output: String is too short
Length is 6, which is smaller than 10.
Approach:
- We will take a string from the user
- We have to find the length of the string so that we can decide whether it is smaller or larger than 10.
- We will find the length using the following command:
len=`echo -n $str | wc -c`
Below is the Implementation:
# Shell script to print message
# when condition doesn't matched
# taking user input
echo "Enter the String : "
read string
# Finding the length of string
# -n flag to avoid new line
# wc -c count the chars in a file
len=`echo -n $string | wc -c`
# Checking if length is smaller
# than 10 or not
if [ $len -lt 10 ]
then
# if length is smaller
# print this message
echo "String is too short."
fi
Output:

Example 2: We will accept two input String and find whether both are equal or not.
String1 -> a = "abc"
String2 -> b = "abc"
Here a and b are equal.
Below is the implementation:
# Shell script to check whether two
# input strings is equal
# take user input
echo "Enter string a : "
read a
echo "Enter string b : "
read b
# check equality of input
if [ "$a" = "$b" ]; then
# if equal print equal
echo "Strings are equal."
else
# if not equal
echo "Strings are not equal."
fi
Output:
Geeks
Geeks
Strings are equal.
Example 3:
We are working with strings, so now we will take two strings as input and check which of the two is greater in respect of lexicographical (alphabetical) order.
string1 > string2 : The greater than operator returns true if the left operand is greater than the right sorted by lexicographical (alphabetical) order.
string1 < string2 : The less than operator returns true if the right operand is greater than the right sorted by lexicographical (alphabetical) order.
# Shell script to check which
# string is lexicographically
# larger
# user input
echo "Enter String a : "
read a
echo "Enter String b : "
read b
# if a is greater than b
if [[ "$a" > "$b" ]]; then
echo "${a} is lexicographically greater than ${b}."
# if b is greater than a
elif [[ "$a" < "$b" ]]; then
echo "${b} is lexicographically greater than ${a}."
# if both are equal
else
echo "Strings are equal"
fi
Output:
abc
def
def is lexicographically greater than abc.
Similar Reads
Shell Script To Check For a Valid Floating-Point Value User Input can be hectic to manage especially if it's about numbers. Let's say you wanted to perform mathematical calculations in your app or script. It's not hard to say that it will have to deal with floating-point numbers. It's easy in many statically typed programming languages but if it has to
4 min read
Conditional Statements | Shell Script Conditional Statements: There are total 5 conditional statements which can be used in bash programming if statement if-else statement if..elif..else..fi statement (Else If ladder) if..then..else..if..then..fi..fi..(Nested if) switch statement Their description with syntax is as follows: if statement
3 min read
How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri
1 min read
Bash shell script to find if a number is perfect or not In this article, we will discuss how to write a bash script to find if a number is perfect or not. A perfect number is defined as, a positive number that is equal to the sum of its proper divisors. Smallest no is 6 ex= 1,2,3 are divisor of 6 and 1+2+3=6 Method 1: Using While LoopRead the input using
3 min read
String Operators | Shell Script Pre-Requisite: Conditional Statement in Shell Script There are many operators in Shell Script some of them are discussed based on string. Equal operator (=): This operator is used to check whether two strings are equal. Syntax:Operands1 = Operand2Example:Â php #!/bin/sh str1="GeeksforGeeks"; str2="ge
2 min read
Shell Script To Check That Input Only Contains Alphanumeric Characters If you want an input which contains only alphanumeric characters i.e. 1-9 or a-z lower case as well as upper case characters, We can make use of regular expression or Regex in short in a Shell Script to verify the input. Example: Input: Geeksforgeeks Output: True Explanation: Here all the inputted d
2 min read
How to prompt for Yes/No/Cancel input in a Linux shell script You may have noticed that shell programs occasionally ask the user for confirmation by prompting [Y/n] or [Yes/No]. Knowing whether a user wishes to continue with the following stages or not is useful. A similar feature can be added to your script as well. This article will assist you with examples
3 min read
How to check if a directory or a file exists in system or not using Shell Scripting? Shell scripting is really a powerful and dynamic way to automate your tasks. To test if a directory or file already exists in the system or not we can use shell scripting for the same along with test command. To proceed with the test script lets first check the test manual. To open a manual use the
2 min read
How to Check the Syntax of a Bash Script Without Running It? A bash script is a text file that contains a sequence of commands that are executed by the bash shell, a Unix-based command-line interface. Bash scripts are used to automate tasks, create utility scripts, and perform a wide range of other functions in the command-line environment. They can include v
5 min read