Functions and Case in Shell Scripting...!!

Riya DeshmukhRiya Deshmukh
2 min read

Workshop Conducted by Pranav Jambare sir.

In today's blog. we are going to learn the following:

  1. Functions

  2. case Statements

Functions:

  • A function is a block of code that is reusable and performs certain operations.

  • Using functions to perform repetitive tasks is an excellent way to create code reuse.

  • Shell functions are similar to subroutines, procedures, and functions in other programming languages.

  • t is used to perform a specific task or a set of instructions.

  • Syntax of Function:

  •    function_name() {
    
      statement
    
      }
    
  • Example :

  •   #!/bin/sh
    
      # Define your function here
      Hello () {
         echo "Hello World"
      }
    
      # Invoke your function
      Hello
      #Upon execution, you will receive the following output −
    
      $./test.sh
      Hello World
    

Case Statement:

  • The case statement allows you to easily check patterns.

  • The bash case statement is the simplest form of the if elif else conditional statement. The case statement simplifies complex conditions with multiple different choices. This statement is easier to maintain and more readable than nested if statements.

  • The case statement tests the input value until it finds the corresponding pattern and executes the command linked to that input value.

  •   case $variable in
      pattern-1)
        commands;;
      pattern-2)
        commands;;
      pattern-3)
        commands;;
      pattern-N)
        commands;;
      *)
        commands;;
      esac
    
  • The case statement starts with the case keyword followed by the $variable and the in keyword. The statement ends with the case keyword backwards - esac.

  • $variable

    The script compares the input $variable against the patterns in each clause until it finds a match.

  • Patterns

    A pattern and its commands make a clause, which ends with ;;.

  • Patterns support special characters.

  • The ) operator terminates a pattern list.

  • The script executes the commands corresponding to the first pattern matching the input $variable.

  • The asterisk * symbol defines the default case, usually in the final pattern.

  • 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
      #output
    
      Choose the colour : 
       1) Red
       2) Green
       3) Blue
       Green
       You have choosen the Green colour..!
    

    Thank You guys for Reading my Blog.....!!

10
Subscribe to my newsletter

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

Written by

Riya Deshmukh
Riya Deshmukh