Linux Workshop 2023-24
Hello Everybody!!
This blog is all about the activities taken on Day - 9 of the Linux Workshop conducted by Pranav Jambare sir at Dr. Babasaheb Ambedkar Technological University, Lonere - Raigad.
Content :
Functions
Case statement
Functions :
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 syntax of function in shell scripting :
function_name () { #commands to be executed #body of the function } function_name #this is function call
The function_name can be any valid string and the body can be any sequence of valid statements in the scripting language.
Example :
#!/bin/bash create_user () { echo "Enter the username : " read username useradd ${username} echo "You have created the user : ${username}" } create_user
Output :
Enter the username : TestUser You have created the user : TestUser
Case statement :
A 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 can 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 any other programming language. But unlike any programming language, the 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 the case statement :
case {expression} in Pattern_Case_1) #Statements to be executed ;; Pattern_Case_1) #Statements to be executed ;; Pattern_Case_N) #Statements to be executed ;; *) #This is used for the cases which are exceptional #Statements to be executed ;; 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 :
#!/bin/bash echo "Choose the colour : " echo "1) Red" echo "2) Green" echo "3) Blue" read colour case {colour} in Red) echo "You have choosen the Red colour..!!" ;; Green) echo "You have choosen the Green colour..!!" ;; Blue) echo "You have choosen the Blue colour..!!" ;; *) echo "The colour you choose is unavailable.." ;; esac
Output :
Choose the colour : 1) Red 2) Green 3) Blue Red You have choosen the Red colour..!!
An example that includes functions and cases:
Example :
#!/bin/bash function1 () { echo "You called function-1" } function2 () { echo "You called function-2" } function3 () { echo "You called function-3" } echo "Choose the function to call : " echo "1) function1" echo "2) function2" echo "3) function3" read functionz case {functionz} in function1) function1 ;; function2) function2 ;; function3) function3 ;; *) echo "You choose undeclared function.." ;; esac
Output :
Choose the function to call : 1) function1 2) function2 3) function3 function2 You called function-2
In the above example, we've declared the three functions and called them using case statements.
This is all for the last day which is Day - 9 of the Linux Workshop conducted by Pranav Jambare sir.
We all enjoyed the workshop and learn a lot of things related to Linux.!!
This was the last blog of the Linux Workshop blog series.
Thank you for reading the blog..!!
Subscribe to my newsletter
Read articles from Shreyas Limaye directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Shreyas Limaye
Shreyas Limaye
Currently studying Diploma in Computer Engineering, at Dr. Babasaheb Ambedkar Technological University, Lonere, Raigad.