Skip to content
Related Articles

Related Articles

Improve Article

Menu Driven Shell Script to Check Memory and Disk Usages

  • Last Updated : 07 Sep, 2021
Geek Week

In Linux most of the time, we automate things using the bash scripts. Some script has only one functionality, but some script can do more than one functions. For that, we have to provide the menu to select the option which function should be performed now. This script is called as Menu-Driven Programs.

Free Commands: This command displays the total amount of free space available along with the amount of memory used and swap memory in the system, and also the buffers used by the kernel.

Syntax:

$free [OPTION]

Option: refers to the options compatible with free command.

Example:



df Commands: The df command (short for disk free), is used to display information related to file systems about total space and available space.

Syntax :

df [OPTION]... [FILE]...

Example:

Let’s see the basic algorithm of the menu-driven program:

  • First, create all functions in the script that are performed by the bash script
  • Then we need to provide the menu to the user from which the user has to select the option. For that create one function like show menu which contains all options with the function’s name.
  • Now we need to read the option which is entered by the user for that we need to create another function that reads the option and processes the input. After taking input use if statement or switch statement but switch statement is good. The switch statement mention all options with functions same as the show menu function. Then switch statement will call the appropriate function according to the choices.
  • Then use a while to call the show menu and take the input function until the user does not want to exit from the program.

Stepwise Approach:

Let’s see one example to understand better. We are going to build one script which performs two functionalities one is to check memory usage and another disk usage. Now let’s move according to the above algorithm.

Step 1: We are going to create two functions that check memory usage and disk usage.



memoryUsage(){
        echo "Memory Usage:"
        free
        read -p "Press any key to Continue...."
}

diskUsage(){
        echo "Disk Usage:"
        df
        read -p "Press any key to Continue...."
}

Now we have created memoryUsage function which gives memory usage details and diskUsage Function which gives the details about disk usage.

Step 2: Now let’s create a Menu to show a user with the required options

show_menu()
{
       clear
       echo "++++++++++++ MENU +++++++++++++"
       echo "1. Show Memory Usage."
       echo "2. Show DIsk Usage."
       echo "3. Exit"
       echo "+++++++++++++++++++++++++++++++"
}

show_menu() function shows the menu to the user which contains three options 1st for memory usage 2nd for disk usage and 3rd to exit from the program.

Step 3: Now let’s create the function which takes input according to the menu and process the given input:

take_input()
{
       local choice
       read -p "Select the option from above menu: " choice

       case $choice in
               1) memoryUsage ;;
               2) diskUsage ;;
               3) exit 0;;
               *) echo "Enter Valid Option!!"
                       read -p "Press any key to Continue...."

               esac
}

take_input function takes the input and passes it to the switch statement in the function .which calls the function according to provided option.

Step 4: Now let’s use the while loop to call the show_menu function and the take_input function until the user does not want to exit.

while true
do
       show_menu
       take_input
done

Below is the full implementation:

#!/bin/bash

# function to show memory usage
memoryUsage(){
       echo "Memory Usage:"
       free
       read -p "Press any key to Continue...."
}

# function to show disk usage
diskUsage(){
       echo "Disk Usage:"
       df
       read -p "Press any key to Continue...."
}

# function to show menu
show_menu()
{
       clear
       echo "++++++++++++ MENU +++++++++++++"
       echo "1. Show Memory Usage."
       echo "2. Show DIsk Usage."
       echo "3. Exit"
       echo "+++++++++++++++++++++++++++++++"
}

# function to take input
take_input()
{
        #take the input and store it in choice variable
       local choice
       read -p "Select the option from above menu: " choice
        
        #using switch case statement check the choice and call function.
       case $choice in
               1) memoryUsage ;;
               2) diskUsage ;;
               3) exit 0;;
               *) echo "Enter Valid Option!!"
                       read -p "Press any key to Continue...."

               esac
       }

# for loop to call the show_menu and take_input function.
while true
do
       show_menu
       take_input
done

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :