Case statement and Functions in Shell Scripting

Om KadamOm Kadam
2 min read

Hello everyone, and welcome back to my channel, I am Om Kadam. I am here with a new shell scripting topic as a case statement and function. The lecture on shell scripting was conducted by Mr Pranav Jambare.

Topic:-

1) Case statement

2) Functions

Case Statement:-

1) The case statement contains the possible responses that correspond to the user's input.

2)In this example, the output is the echo command output. The last line, esac, ends the case statement.

3) A case statement must start with the case keyword and end with the esac keyword.

4)The expression is evaluated and compared with the patterns in each clause until a match is found. The statement or statements in the matching clause are executed.

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

6) Example :

  1.   #!/bin/bash
    
      echo "select the Car : "
      echo "1) BMW"
      echo "2) Audi"
      echo "3) Mercedes-Benz"
      read car
    
      case {car} in
      BMW)
          echo "You have select the BMW..!!"
      ;;
      Audi)
          echo "You have select the Audi..!!"
      ;;
      Mercedes-Benz)
          echo "You have select the Mercedes-Benz..!!"
      ;;
      *)
          echo "The car you select is not available.."
      ;;
      esac
    
  2. Output :

      select the car : 
      1) BMW
      2) Audi
      3) Mercedes-Benz 
      Mercedes-Benz 
      You have select the Mercedes-Benz ..!!
    

Functions:-

1) A function is a collection of statements that execute a specified task. Its main goal is to break down a complicated procedure into simpler subroutines that can subsequently be used to accomplish the more complex routine.

2) The functions that return a value to the caller. The return keyword is used by the functions for this purpose.

3) The function_name can be any valid string and the body can be any sequence of valid statements in the scripting language.

Example:-

  1.   #!/bin/bash
    
      create_car () {
          echo "Enter the carname : "
          read carname
          adduser ${carname}
          echo "You have create the car : ${carname}"
      }
      create_car
    
  2. Output :

      Enter the carname : 
      Mercedes-Benz 
      You have create the car : Mercedes-Benz
    

At the end of the Linux workhop Day-9. We well enjoyed in the last day of workshop.

We learn more things about Linux. And this is the lasst blog of our Linux.

Thanks for reading my blog...!!

0
Subscribe to my newsletter

Read articles from Om Kadam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Om Kadam
Om Kadam