Linux Workshop Day - 9
Hello Everyone
Myself kaushal shinde
I'm back with the new topics covered by us in the 9th day of this workshop here we go with the topic.
#learnwithpra9
Content :
Functions
Case statement
Functions:-
1) A function is a collection of statements that execute a specified task.
2) Its main goal is to break down a complicated procedure into simpler subroutines that can subsequently be used to accomplish the more complex routine.
3) For the following reasons, functions are popular:
i) Assist with code reuse.
ii)Enhance the program’s readability
iii)Modularize the software.
iv)Allow for easy maintenance.
4) The basic syntax of function in shell scripting :
function_name () {
#commands to be executed
#body of the function
}
function_name #this is function call
5)The function_name can be any valid string and the body can be any sequence of valid statements in the scripting language.
6) Example :
#!/bin/bash
create_user () {
echo "Enter the username : "
read username
useradd ${username}
echo "You have created the user : ${username}"
}
create_user
7) 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
6. 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.
7. 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
8. 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
2. Output:-
Choose the function to call :
1) function1
2) function2
3) function3
function2
You called function-2
3. 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.
Thank you for reading the blog.
Subscribe to my newsletter
Read articles from Kaushal Shinde directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Kaushal Shinde
Kaushal Shinde
Computer Engineer