Learning the concepts of Linux

Hello everyone!! I'm back with my last blog of the Linux series. This was my last day(Day-9) of the Linux Workshop, it helped me learn and explore new things related to Linux. I would like to especially thank Pranav Jambare Sir for initiating such an elaborative workshop about Linux.

So, here are the topics covered during the Linux Workshop Day-9:

  1. Functions

  2. Case Statements

Functions

  • Functions in Linux shell scripting are blocks of reusable code that can be called multiple times within a script.

  • They are used to perform specific tasks, such as printing a message or performing a calculation and they help to make scripts more modular and easier to read, write, and maintain.

  • Functions can accept arguments, which are values passed to the function when it is called and can return values, which are values calculated by the function and returned to the calling code.

  • Syntax ->

    function_name() {

    #code to be executed

    }

  •   Example ->
    
      #!/bin/bash
    
      function_name() {
    
      echo "Hello, this is just an example of functions."
    
      }
    

Case statements

  • Case statements in Linux shell scripting are a way to simplify complex if-else statements.

  • They are used to perform different actions based on the value of a variable or expression.

  • The case statement compares the value of the variable against a list of possible values and executes the code block that corresponds to the first matching value.

  • Case statements can be used to make the code more readable and easier to maintain.

  • Syntax ->

    case $ (word) in

    1)

    # Code to execute if the expression matches pattern1

    ;;

    2)

    # Code to execute if the expression matches pattern2

    ;;

    *)

    # Code to execute if the expression does not match any of the above patterns

    ;;

    esac

  •   Example ->
      #!/bin/bash
    
      echo "Enter a number between 1 and 3:" 
    
      read num
    
      case $num in
    
      1) echo "You entered one." 
    
      ;; 
    
      2) echo "You entered two." 
    
      ;; 
    
      3) echo "You entered three.": 
    
      ;; 
    
      *) 
    
      echo "Invalid input." 
    
      ;; 
    
      esac
    

Thank You for reading my blogs!!!

10
Subscribe to my newsletter

Read articles from Richa Sandesh Patil directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Richa Sandesh Patil
Richa Sandesh Patil