Conditional Statement in Linux Shell Programming

1085
Conditional Statement in Linux Shell Programming

Command-line Basics: Shell Script Conditionals

Most computer applications, irrespective of terminology, are only a lot of third-party statements. These conditionals permit the control flow to be changed instead of merely running every code each time. The exterior of computer programming, both conditionals are all about us, only a succession of choices being made and their results.

Maybe we really are living inside a simulation. O_O

Getting started

To conduct the next shell scripting commands, then you’ll require access to some system using a shell such as bash or zsh accessible. That can be a typical problem on Unix-like systems such as Linux and macOS and may be retrieved on Windows and Chrome OS using the various Linux subsystem offerings.

Though we speak about broadcasts, the source code we’ll be composing can be input straight to your shell prompt and implemented. Should you chance to goof while studying something at all, do not worry. You may just hit CTRL-C to return to a fresh instance.

Also See:  Shell Script to Connect to Oracle Database in Linux

Basic conditional structure

The Same as All the conditionals you have encountered in several other languages, conditionals in shell scripting include precisely the same “if,” “else,” and “else if” states, using the slightly different syntax:

Conditional Statements: Five conditional statements can be Utilized in celebration programming

  1. if statement
  2. if-else statement
  3. if..elif..else..fi statement (Else If ladder)
  4. if..then..else..if..then..fi..fi..(Nested if)
  5. switch statement

Their description, together with syntax, is as follows:

if statement

This cube will undergo the procedure if a given condition is true.

Syntax:

if [ expression ]
then
statement
fi

If-else statement

If the given condition isn’t true is if apart, then else area will be implemented.

Syntax

if [ expression ]
then
statement1
else
statement2
fi

if..elif..else..fi statement (Else If ladder)

To utilize many conditions in 1 if-else block, then the elif keyword is used in the shell. If expression1 is true, then it implements statements 2 and 1, and this procedure persists if not one of this problem is true, then it procedures else area.

Also See:  Windows 11 Android apps support is coming much later

Syntax

if [ expression1 ]
then
statement1
statement2
.
.
elif [ expression2 ]
then
statement3
statement4
.
.
else
statement5
fi

if..then. .else. .if. .then. .fi. .fi. . (Nested if)

A nested if-else block may be utilized when one condition is suits then it checks another ailment. If expression1 is false, then it procedures else area and expression2 will be assessed from the syntax.

Syntax:

if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi

switch statement

Case statement functions as a change statement if given value match using the routine then it can implement a block of that specific pattern

When a match has been located, all the related statements before the dual semicolon (;-RRB- have been implemented.

A case is going to be declared when the previous command is implemented.

When there’s not any game, the exit condition of the instance is zero.

Syntax:

case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac

Also See:  Activate NBA League Pass Apple Tv

Example Programs

Example 1:Implementing if statement

#Initializing two variables
a=10
b=20

#Check whether they are equal
if [ $a == $b ]
then
echo “a is equal to b”
fi

#Check whether they are not equal
if [ $a != $b ]
then
echo “a is not equal to b”
fi

Output

$bash -f main.sh
a is not equal to b

Example 2:Implementing if.else statement

#Initializing two variables
a=20
b=20

if [ $a == $b ]
then
#If they are equal then print this
echo “a is equal to b”
else
#else print this
echo “a is not equal to b”
fi

Output

$bash -f main.sh
a is equal to b

Example 3:Implementing switch statement

CARS=”bmw”

#Pass the variable in string
case “$CARS” in
#case 1
“mercedes” ) echo “Headquarters – Affalterbach, Germany” ;;

#case 2
“audi” ) echo “Headquarters – Ingolstadt, Germany” ;;

#case 3
“bmw” ) echo “Headquarters – Chennai, Tamil Nadu, India” ;;
esac

Output

$bash -f main.sh
Headquarters – Chennai, Tamil Nadu, India.