Functions And Case In Shell Scripting !!!!
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 in Linux Shell
Linux shell functions allow you to package a set of commands into one code block which can be called any number of times. This makes your shell programs small in length and increases the re-usability of code.
In Another word we can say that Function is a reusable block of code. Often we put repeated code in a function and call that function from various places. A library is a collection of functions. We can define commonly used functions in a library and other scripts can use them without duplicating code.
The basic syntax of function in shell scripting :
function_name () { #commands to be executed #body of the function } function_name #this is function call
- Let's use Linux functions to print Hello World
#!/bin/bash
#define the function
hello_world () {
echo "hello world"
}
#call the function
hello_world
You must always first define the function before calling it. By simply giving the function name, you are calling/executing the function.
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 :
Arya123
You have created the user : Arya123
Case statement :
The bash case
statement is generally used to simplify complex conditionals when you have multiple different choices. Using the case
statement instead of nested if
statements will help you make your bash scripts more readable and easier to maintain.
The Bash case
statement has a similar concept to the Javascript or C switch
statement. The main difference is that, unlike the C switch
statement, the Bash case
statement doesn’t continue to search for a pattern match once it has found one and executed statements associated with that pattern.
case
Statement Syntax
The Bash case
statement takes the following form:
case EXPRESSION in
PATTERN_1)
STATEMENTS
;;
PATTERN_2)
STATEMENTS
;;
PATTERN_N)
STATEMENTS
;;
*)
STATEMENTS
;;
esac
Each
case
statement starts with thecase
keyword, followed by the case expression and thein
keyword. The statement ends with theesac
keyword.You can use multiple patterns separated by the
|
operator. The)
operator terminates a pattern list.A pattern can have special characters.
A pattern and its associated commands are known as a clause.
Each clause must be terminated with
;;
.The commands corresponding to the first pattern that matches the expression are executed.
It is a common practice to use the wildcard asterisk symbol (
*
) as a final pattern to define the default case. This pattern will always match.If no pattern is matched, the return status is zero. Otherwise, the return status is the exit status of the executed commands.
Case Statement Example
#!/bin/bash
echo "Which color do you like best?"
echo "1 - Blue"
echo "2 - Red"
echo "3 - Yellow"
echo "4 - Green"
echo "5 - Orange"
read color;
case $color in
1)
echo "Blue is a primary color."
;;
2)
echo "Red is a primary color."
;;
3)
echo "Yellow is a primary color."
;;
4)
echo "Green is a secondary color."
;;
5)
echo "Orange is a secondary color."
;;
*)
echo "This color is not available. Please choose a different one."
;;
esac
#Output
echo "Which color do you like best?"
1 - Blue
2 - Red
3 - Yellow
4 - Green
5 - Orange
5
Orange is a secondary color.
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
This is all for the last day which is Day - 9 of the Linux Workshop conducted by Pranav Jambare sir.
This was the last blog of the Linux Workshop blog series.
Thank you for reading the blog..!!
Subscribe to my newsletter
Read articles from Arya Mahendra Karade directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by