Discovering the concept of Linux

Discovering the concept of Linux

Linux workshop:- day 9

I am Teertha Darekar
Welcome back to my last day of workshop conducted by Pranav Jambare sir.

CONTENTS:-

Functions

Cases

Let's begin ;

--> Functions

  • Functions enable you to break down the overall functionality of a script into smaller, logical subsections, which can then be called upon to perform their individual tasks when needed.

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

  • It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient.

  • The function name is used to call the function from elsewhere in your scripts.

Syntax:-

function_name () { 
   statement
}

Example:-

#!/bin/bash

just_echo() {
    echo " it's just a echo statement nothing else "
}

create_file () {
    echo " enter the file name "
    read name
    touch /tmp/${name}
}

just_echo
create_file

-->Case

  • The case statement simplifies complex conditions with multiple different choices.

  • This statement is easier to maintain and more readable than nested if statements.

  • The basic syntax of the case...esac statement is to give an expression to evaluate and to execute several different statements based on the value of the expression.

Syntax:-

case ${word} in
   1)
      Statements
      ;;
   2)
      Statements
      ;;
   3)
      Statements
      ;;
   *)
      Statements
     ;;
esac

Here the string word is compared against every pattern until a match is found.

The statements following the matching pattern execute.

If no matches are found, the case statement exits without performing any action.

When the statements part executes, the command ;; indicates that the program flow should jump to the end of the entire case statement.

() - used to run commands

{} - used to call value

Example 1:-

#!/bin/bash
echo " enter the colour "
read col
case ${col} in
1) 
    echo " It's a red colour "
;;
2)
    echo " It's a black clour "
;;
*)
    echo " It's an Invalid colour "
;;
esac

Example 2:-

#!/bin/bash
echo "1. file create "
echo "2. display file "
echo "3. current directory "
echo "4. make dir "
read input

case ${input} in
1)
   echo "enter the name of the file "
   read filename
   touch /tmp/${filename}
;;
2)
   echo " enter the name of the file "
   read filename
   ls -l /tmp/${filename}
;;
3)
   pwd
;;
4)
   echo " enter the name of dir "
   read dir
   mkdir /tmp/${dirname}
;;
*) 
   echo " Invalid Input "
;;
esac

Example 3:-

#!/bin/bash

function1()
{
   echo " function 1 is called "
}
function2()
{
   echo " function 2 is called "
}
function3()
{
   echo " function 3 is called "
}
function4()
{
   echo " function 4 is called "
}
function5()
{
   echo " function 5 is called "
}

echo " enter the function number to be called  "
read func
case ${func} in
1)
   function1
;;
2)
   function2
;;
3)
   function3
;;
4)
   function4
;;
5)
   function5
;;
*)
   echo " Invalid function "
;;
esac

And the last day of the workshop has been ended with a great experience and learning Linux.

Got to know about most of the concepts in Linux throughout the whole workshop.

Thank you ...