Bash Script -Part 2

Gokul
3 min readJan 7, 2023

--

Bash Script 2

ARITHMETIC OPERATORS

There are 11 arithmetic operators which are supported by Bash Shell.

Those are ,

  1. Addition
  2. Subtraction
  3. Multiplication
  4. Division
  5. Exponentiation
  6. Modulo
  7. Increment and Decrement

Double parentheses is the easiest mechanism to perform basic arithmetic operations in the Bash shell.

Syntax

((expression))

#Method 1

Sum=$((10+3))
echo "Sum = $Sum"

#Method 2

((Sum=10+3))
echo "Sum = $Sum"

#Method 3

Num1=10
Num2=3
((Sum=Num1+Num2))
echo "Sum = $Sum"

#Method 4

Num1=10
Num2=3
Sum=$((Num1+Num2))
echo "Sum = $Sum"

CONTROL STATEMENT

Control Statements works based on the Condition

Control Statements are two type.Those are,

  1. Conditional Statement
  2. Looping Statement
  3. Jumping Statement

IF Statement

Syntax :

if [ expression ];

then

statements

fi

#EXAMPLE For IF statement
read -p " Enter number : " number

if [ $number -gt 125 ]
then
echo "Value is greater than 125"
fi

BASH IF ELSE

Syntax :

if [ condition ];

then

<if block commands>

else

<else block commands>

fi

#!/bin/bash  

#when the condition is true
if [ 10 -gt 3 ];
then
echo "10 is greater than 3."
else
echo "10 is not greater than 3."
fi

#when the condition is false
if [ 3 -gt 10 ];
then
echo "3 is greater than 10."
else
echo "3 is not greater than 10."
fi

Bash Nested If Statement

#!/bin/bash
read -p "Enter a value:" value
if [ $value -gt 9 ];
then
if [ $value -lt 11 ];
then
echo "$value>9, $value<11"
else
echo "The value you typed is greater than 9."
fi
else echo "The value you typed is not greater than 9."
fi

BASH ELSE IF

Syntax :

if [ condition ];

then

<commands>

elif [ condition ];

then

<commands>

else

<commands>

fi

#!/bin/bash  

read -p "Enter a number of quantity:" num

if [ $num -gt 100 ];
then
echo "Eligible for 10% discount"
elif [ $num -lt 100 ];
then
echo "Eligible for 5% discount"
else
echo "Lucky Draw Winner"
echo "Eligible to get the item for free"
fi

BASH CASE

Syntax :

case expression in

pattern_1)

statements

;;

pattern_2)

statements

;;

pattern_3|pattern_4|pattern_5)

statements

;;

pattern-n)

statements

;;

*)

statements

;;

esac

#!/bin/bash  

echo "Which Operating System are you using?"
echo "Windows, Android, Chrome, Linux, Others?"
read -p "Type your OS Name:" OS

case $OS in
Windows|windows)
echo "That's common. You should try something new."
echo
;;
Android|android)
echo "This is my favorite. It has lots of applications."
echo
;;
Chrome|chrome)
echo "Cool!!! It's for pro users. Amazing Choice."
echo
;;
Linux|linux)
echo "You might be serious about security!!"
echo
;;
*)
echo "Sounds interesting. I will try that."
echo
;;
esac

LOOPING STATEMENTS

Loops used to perform repetitive tasks.There are three types are available.Those are,

  1. For Loop
  2. While Loop
  3. Until -While Loop

FOR LOOP

Syntax -1

for variable in list

do

commands

done

Syntax -2

for (( expression1; expression2; expression3 ))

do

commands

done

    #!/bin/bash  
#For Loop to Read Three-expression

for ((i=1; i<=10; i++))
do
echo "$i"
done

WHILE LOOP

The bash while loop can be defined as a control flow statement which allows executing the given set of commands repeatedly as long as the applied condition evaluates to true.

Syntax

while [ expression ];

do

commands;

multiple commands;

done

    #!/bin/bash  
#Script to get specified numbers

read -p "Enter starting number: " snum
read -p "Enter ending number: " enum

while [[ $snum -le $enum ]];
do
echo $snum
((snum++))
done

echo "This is the sequence that you wanted."

UNTIL LOOP

Bash Until Loop in a bash scripting is used to execute a set of commands repeatedly based on the boolean result of an expression

until [ expression ];

do

….

Statements

…..

done

#!/bin/bash  
#Bash Until Loop example with multiple conditions

max=5
a=1
b=0

until [[ $a -gt $max || $b -gt $max ]];
do
echo "a = $a & b = $b."
((a++))
((b++))
done

JUMPING STATEMENT

Break -It Terminate the process of execution

#!/bin/bash  
#Table of 2

for table in {2..100..2}
do
echo $table
if [ $table == 20 ]; then
break #break statement terminate the execution
fi
done

Continue -This keyword used to skip the execution

    #!/bin/bash  
#Numbers from 1 to 20, ignoring from 6 to 15 using continue statement"

for ((i=1; i<=20; i++));
do
if [[ $i -gt 5 && $i -lt 16 ]];
then
continue
fi
echo $i
done
   echo "Thank you for being part of my Blog PAge"

Thank You for Reading My Article .Learn more information to grab the opportunities.

--

--

Gokul
Gokul

Written by Gokul

Cybersecurity Enthusiast | Smart India Hackathon |TN Police Hackathon Finalist | Linux | WebApp Penetration Tester | CCNA |Intern At Coimbatore CyberCrime Dept

No responses yet