| by Arround The Web | No comments

How to Use the Bash “If –Z” and “If –N” Statements to Test the String

The “if” statement is used in Bash for conditional branching like in other programming languages. The “if” statement can be used to check the different conditions in Bash. The different types of comparison operators, logical operators, and options are used with the “if” statement for testing.  The uses of the “-z” and “-n” option to test the string values using the “if” statement in Bash are shown in this tutorial.

Using the “If -Z” Statement

Sometimes, it is required to check if a string variable is empty or if it contains a string of zero length. There are many options in Bash to do this task. Using the “-z” option with the “if” statement is one of the ways to check whether a variable is empty or not.  The uses of the “if -z” option are shown in the next part of the tutorial.

Example 1: Checking the “If -Z” Statement Using “[]”

Create a Bash file with the following script where the use of the “if –z” statement with the “[]” is shown. Two inputs are taken from the user. If any of the input values is empty, an error message is printed. Otherwise, the sum of the two input values are calculated and printed.

#!/bin/bash
#Read the first number
read -p "Enter the first number : " n1
#Read the second number
read -p "Enter the second number : " n2

#Check any input is empty or not
if [[ -z "$n1" || -z "$n2" ]]
then
    #Print the error message
    echo "The first or second number is empty."
else
    #Calculate the sum
    sum=$(($n1+$n2))
    #Print the summation value
    echo "The sum of $n1 and $n2 is $sum."
fi

 
Output:

The following output appears if the script is executed without any argument:


The following output appears if the script is executed with one argument:


The following output appears if the script is executed with two arguments:


Example 2: Checking the “If -Z” Statement with the “Test” Command

Create a Bash file with the following script where the use of the “if –z” statement with the “test” command is shown. One input is taken from the user that must be numeric. If the input value is empty, an error message is printed. If the input value is non-empty but not contains a numeric value, another error message is printed. If an integer number is taken as an input, the input value is printed.

#!/bin/bash
#Read the age value
read -p "Enter your age: " age

#Check whether the input value is empty or not
if test -z "$age"
then
    #Print the error message for empty
    echo "The input value is empty."
    exit
fi

#Check whether the input value is a number or not
if ! [[ $age =~ ^[0-9]+$ ]]; then
    #Print the error message for non-numeric data
    echo "The age value must be a number."
else
    #Print the input value
    echo "You are $age years old."
fi

 
Output:

The following output apepars if the script is executed with the numeric input value:


The following output appears if the script is executed with the string input value:

Using the “If -N” Statement

Sometimes, it is required to check if a string variable is non-empty or if it contains a string value more than zero length. There are many options in Bash to do this task. Using the “-n” option with the “if” statement is one of the ways to check whether a variable is non-empty or not.  The uses of the “if -n” option are shown in the next part of the tutorial.

Example 1: Checking the “If -N” Statement Using “[]”

Create a Bash file with the following script where the use of the “if –n” statement with the “[]” is shown. One input value is taken from the user. If the input value is empty, an error message is printed. Otherwise, the value of the input is printed.

#!/bin/bash
#Read the book name
read -p "Enter the book name: " book

#Check if the input is non-zero or not
if ! [ -n "$book" ]
then
    #Print the error message
    echo "Book name can't be empty."
else
    #Print the input value
    echo "The book name is $book."
fi

 
Output:

The following output appears if the script is executed with any input value:


The following output appears if the script is executed with a string input value:


Example 2: Checking the “If -N” Statement with the “Test” Command

Create a Bash file with the following script where the use of the “if –n” statement with the “test” command is shown. The first input is a string and the second input is numeric. If both input values are non-empty, an error message is printed. If the second input value is non-numeric, otherwise, the input values are printed.

#!/bin/bash
#Read the book name
read -p "Enter the book name: " book

#Read the book name
read -p "Enter the book price: " price

#Check whether the input is non-zero or not
if test -n "$book" && test -n "$price"
then
   #Check whether the input value is a number or not
   if ! [[ $price =~ ^[0-9]+$ ]]; then
       #Print the error message for non-numeric data
       echo "The price value must be a number."
   else
       #Print the input values
       echo "Book name is $book."
       echo "Book price is $price."
   fi
else
   #Print the error message
   echo "Book name or price can't be empty."
fi

 
Output:

The following output appears if the script is executed with two empty input values:


The following output appears if the script is executed with one empty input value:


The following output appears if the script is executed with two non-empty valid input values:


The following output appears if the script is executed with two non-empty values and an invalid price value:

Conclusion

The uses of both “if –z” and “if –n” statements are explained in this tutorial using multiple examples to help the Bash users to properly apply these statements in their script.

Share Button

Source: linuxhint.com

Leave a Reply