Skip to main content

Command Palette

Search for a command to run...

Basic concepts of linux

Updated
3 min read
Basic concepts of linux

Day 9

Hello Everyone!!!

Myself Swanik Santosh Gudekar and I am back with the last blog on Linux workshop. This is the ninth blog of Linux series. Today I attended the ninth and last session of Linux workshop conducted by Master Pranav Jambare at Dr. Babasaheb Ambedkar Technological University. It was an exciting and informative workshop

Points Covered In This Session :

  • Function

  • Case Statement

Function :

  • A function in Linux is a way of grouping commands or statements that perform a specific task.

  • You can use functions to make your shell scripts more modular and reusable.

  • A function can be declared by using the keyword function before the name of the function.

  • A function can also be declared by using parenthesis after the name of the function.

  • Different statements can be separated by a semicolon or a new line.

  • SYNTAX :

      function_name
      {
        statement
      }
    
  • EXAMPLE :

      #!/bin/sh
    
      first_call () {
         echo "This is the first function speaking..."
      }
    
      second_call () {
         echo "This is now the second function speaking..."
      }
    
      first_call
      second_call
    

Case :

  • The case statement is a way of executing different commands based on the value of a variable or an expression.

  • The case statement starts with the keyword case followed by the variable or expression and the keyword in.

  • The case statement ends with the keyword esac, which is case spelled backward.

  • The case statement tries to match the variable or expression with each pattern, starting from the top. If a match is found, the commands in that clause are executed and the case statement ends.

  • The pattern *) is used as the default clause, which is executed if none of the other patterns match. It is usually placed at the end of the case statement.

  • Each clause must end with a double semicolon ;; , which indicates the end of the commands for that pattern.

  • SYNTAX :

      case $(word) in
      1)
        statement
        ;;
      2)
       statement
       ;;
      3)
       statement
       ;;
      *)
       statement
       ;;
      esac
    
  • EXAMPLE :

      Program :
    
      #!/bin/bash
    
      echo "1. Dog"
      echo "2. Cat"
      echo "3. Lion"
      echo "4. Tiger"
      read input
      case $(input) in
      1)
       echo "Your favourite animal is Dog !!"
       ;;
      2)
       echo "Your favourite animal is cat !!"
       ;;
      3)
       echo "Your favourite animal is Lion !!"
       ;;
      4)
       echo "Your favourite animal is Tiger !!"
       ;;
      *)
       echo "Invalid Input"
       ;;
      esac
    

    These were the topics covered by Master Pranav Jambare in the ninth session of Linux workshop. Thank you for reading my blog this was the last blog of Linux workshop. It was an exciting, Informative plus fun workshop.