Linux Workshop


Hello everyone....!!!
I'm back here with my another blog and it's the last blog of the Linux workshop series conducted by Mr.Pranav Jambare sir.
In this blog we going to see functions and case in Linux.
Let's start..!!!
Contents
Function
Case
Function
A function is a collection of statements that execute a specified task.
The function is a command in Linux that is used to create functions or methods.
Shell functions are similar to subroutines, procedures, and functions in other programming languages.
The function can be created in the user’s current shell session or saved in the shell startup file to make them available every time a new shell session is initiated
The function_name can be any valid string and the body can be any sequence of valid statements in the scripting language.
Syntax :-
function_name () {
#commands to be executed
}
Example :-
#!/bin/bash # Define your function here Hello () { echo "Welcome user" } # Invoke your function welcome #Output welcome user
Case
case command in Linux is the best alternative when we had to use multiple if/elif on a single variable.
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.
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.
Using
case
is much easier than writing manyif
andelif
statements nested together, so if you have many conditions to test for,case
is the better option.It is used to execute commands based on pattern matching.
The
case
statement tests the input value until it finds the corresponding pattern and executes the command linked to that input value.Syntax :−
case $variable in pattern_1) #statement ;; pattern_2) #statement ;; pattern_3) #statement ;; pattern_N) #statement ;; *) #statement ;; esac
Example :-
#!/bin/bash echo "Choose your favourite color" echo "1 - Blue" echo "2 - Red" echo "3 - Yellow" echo "4 - Green" echo "5 - Orange" read colour; case $colour in 1) echo "Blue is your Favourite color.";; 2) echo "Red is your Favourite colour.";; 3) echo "Yellow is your Favourite colour.";; 4) echo "Green is your Favourite colour.";; 5) echo "Orange is your Favourite colour.";; *) echo "This colour is not available .";; esac
Output :-
#Output Choose your Favourite color 1 - Blue 2 - Red 3 - Yellow 4 - Green 5 - Orange 1 Blue is your Favourite colour.
So these are the topics that were covered by Sir. Pranav Jambare sir. This is the last blog of linux workshop series.
Special thanks to Mr.Pranav Jambare sir we all have learned lots of things related to Linux and we all enjoyed the workshop...!!!
Thank you for Reading...!!!
Subscribe to my newsletter
Read articles from Ayush Bhosale directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Ayush Bhosale
Ayush Bhosale
Student at Dr. Babasaheb Ambedkar University Lonere, in computer science