Linux Workshop 2023-24

Shreyas LimayeShreyas Limaye
3 min read

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 :

  1. Functions

  2. 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:

    • Assist with code reuse.

    • Enhance the program’s readability.

    • Modularize the software.

    • 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 :

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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:

  1. 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.!!

This was the last blog of the Linux Workshop blog series.

Thank you for reading the blog..!!


20
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.