I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…
Shell scripts often need to be constructed to execute different instructions depending on the value of specific control variables. The different paths of execution are specified using conditional instructions.
In this tutorial, we will see about relational operators, and shell decision-making using various conditional statements.
Unix Video #15:
Unix Conditional Statements The if-elif-fi
Unix provides a number of relational operators in addition to the logical operators mentioned earlier. These can be used to compare numeric values.
-lt less than
-le less than or equal to
-gt greater than
-ge greater than or equal to
-eq equal to
-ne not equal to
Unix provides a number of ways for conditionally executing the other commands.
These are covered below:
#1) The if statements
Example:
if <control command>
then
<statements>
fi
#2) The if…else statements
Example:
if <control command>
then
<statements>
else
<statements>
fi
#3) The if…elif…else…fi statement
Example:
if <control command>
then
<statements>
elif
then
<statements>
else
<statements
fi
Given below are some example programs that illustrate these conditional statements:
#1) Check if an input number is positive:
$ echo “Enter a number”
$ read num
$ if [ $num -gt 0 ]
$ then
$ echo “It is a positive number”
$ fi
#2) Check if an input number is positive or not:
$ echo “Enter a number”
$ read num
$ if [ $num -gt 0 ]
$ then
$ echo “It is a positive number”
$ else
$ echo “It is not a positive integer”
$ fi
#3) Check if an input number is positive, zero or negative:
$ echo “Enter a number”
$ read num
$ if [ $num -gt 0 ]
$ then
$ echo “It is a positive number”
$ elif [ $num -eq 0 ]
$ then
$ echo “num is equal to zero”
$ else
$ echo “It is not a positive integer”
$ Fi
Check our upcoming tutorial to know more about Conditional Statements!!
Python Operators with Types and Examples: Python Data Types were explained in detail along with their classification in our previous tutorial. In this tutorial, we learn all about Python Operators along with their types. Simple examples pertaining to each type of Python operator are included in this tutorial. This Python…
Working with Shell Arithmetic and Boolean Operators in Unix: In this tutorial, we will review the various operators that are supported by the Unix shell. Operators are used for manipulating variables and constants in shell programs. They are required to perform mathematical operations. Here, we will explain to you more…
Tutorial On Conditional and Decision Making Statements in C#. This Tutorial will Explain How to Use If, If-Else, If-ElseIf, and Nested If Statements: Classes, Objects, and Methods in C# were explained in detail in our previous tutorial. A class is basically the blueprint of data and objects are the instances…
Overview of Pipes in Unix Programming: In this tutorial, we will learn more about Unix Pipes. Later, we will work with some of the remaining filter commands and see an example of piping them together. Unix Video #20: Pipes in Unix A series of filter commands can be piped together…
This Python if statement video tutorial explains if-else, elif, nested if, and elif ladder statements in Python with programming examples: When we consider our real-time scenario every day, we make some decisions and based on the decisions made we will take further actions. Hence all our daily life activities depend…
Overview of Unix Switch Case Statements: In this tutorial, we will see how a switch case block can be used when creating conditional flow depending on the various values of a single expression. Unix Shell The case-esac Statement Unix Video #16: The Shell Switch Case Syntax and Examples: case <word> in…
Introduction to Unix Operating System: Let's start with Tutorial #1: 'What is Unix' in this series. In this tutorial, you will be able to understand the basic concepts of operating systems, the features of Unix, along its Architecture. => Click here for the Complete Unix Tutorial series Unix Video #1:…
Overview of Unix Filters Text Processing Utilities: In this tutorial, we will learn about filters and work with various filter commands. Filters are commands that read input from stdin and write output to stdout. By default, when using a shell terminal, the stdin is from the keyboard, and the stdout is…