Functions and Case in Shell Scripting !!

Hello Everyone 👋☺️ Sejal Marawade here !! Welcome back to another blog. In this blog, I will cover all the relevant points that have been covered in today's Linux Workshop, carried out by Pranav Jambare Sir .

On the last day of the workshop, the following points were covered.

•Functions in shell scripting :

A function is a collection of statements that execute a specified task. Its main goal is to break down a complicated procedure into simpler subroutines that can subsequently be used to accomplish the more complex routine.For the following reasons, functions are popular:

•Assist with code reuse.

•Enhance the program’s readability.

•Modularize the software.

•Allow for easy maintenance.

•The basic structure of a function in shell scripting looks as follows:

#Syntax:

Function_Name () {

#Statements

}

Example 1:

# A function to print "Hello Linux" on terminal

#!/bin/bash

#Function Defination

print_Hello () {

echo "Hello Linux"

}

#Function Call

print_Hello

•Example 2 :

#Example to find the average of three numbers

#!/bin/sh

find_avg(){

echo "Enter the three numbers :"

read n1

read n2

read n3

sum=$(( $n1 + $n2 + $n3 ))

average=$(( sum / 3 ))

echo "Average of three numbers is ${average}"

}

find_avg

•Case Statement :

•Case statement in bash scripts is used when a decision has to be made against multiple choices. In other words, it is useful when an expression has the possibility to have multiple values.

•This methodology can be seen as a replacement for multiple if-statements in a script. Case statement has an edge over if-statements because it improves the readability of our code and they are easier to be maintained.

• Case statements in a Bash script are quite similar to Case statements in C language. But unlike C, Bash Case statement stops continuing the search as soon as the match occurs. In simple words, they don’t require any break statement that is mandatory to be used in C to stop searching for a pattern further.

The basic syntax of a case statement is given below,

#Syntax:
case EXPRESSION in

Pattern_Case_1)

#STATEMENTS

;;

Pattern_Case_1)

#STATEMENTS

;;

Pattern_Case_N)

#STATEMENTS

;;

*)

#STATEMENTS

;;

esac

A case statement begins with the case keyword which is followed by an expression and the in the keyword and it ends with the esac keyword.

•Example 1 :

#!/bin/bash

echo "Enter the score"

read score

case ${score} in

echo "You are on bronze"

;;

echo "You are on Silver"

;;

echo "You are on Gold"

;;

echo "You are on Platinum"

;;

echo "You are on Diammond"

;;

*)

echo "You are on higher rank or you are not a val

id player"

;;

esac

•Example 2 :

#!/bin/bash

f1()

{

echo "This is function 1."

}

f2()

{

echo "This is function 2."

}

f3()

{

echo "This is function 3."

}

echo "Enter the function name: "

read fn

case ${fn} in

f1)

f1

;;

f2)

f2

;;

f3)

f3

;;

*)

echo "Invalid function name."

;;

esac

That's it for day 9 (last day)...

Hope you have Enjoyed the blog, Thank You for reading !!!

0
Subscribe to my newsletter

Read articles from CO 50 MARAWADE SEJAL directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

CO 50 MARAWADE SEJAL
CO 50 MARAWADE SEJAL