Home » Linux/Unix » Linux/Unix: Simple Menu Driven Program Using Shell Script

Linux/Unix: Simple Menu Driven Program Using Shell Script

The following is an example of a simple menu-driven program using a shell script in Linux/Unix systems.

Linux/Unix: Simple Menu-Driven Program

The following shell script will display the admin menu and will show three menu options to display disk space, logged in users, and memory usage. The shell script is having four functions, three to perform the requested tasks and one to display the simple menu. Do while loop used to loop through the choices until a user chose 0 to exit.

simplemenu.sh

#!/bin/bash

function chkdiskspace {
	clear
	df -h
}

function loggedusers {
	clear
	who
}

function memousage {
	clear
	cat /proc/meminfo
}

function menu {
	clear
	echo
	echo -e "\t\t\tAdmin Menu\n"
	echo -e "\t1. Display Total Disk Space"
	echo -e "\t2. Display All Logged in Users"
	echo -e "\t3. Display Memory Usage"
	echo -e "\t0. Exit Menu\n\n"
	echo -en "\t\tEnter an Option: "
	read -n 1 option
}

while [ 1 ]
do
	menu
	case $option in
	0)
	break ;;
	1)
	chkdiskspace ;;

	2)
	loggedusers ;;

	3)
	memousage ;;

	*)
	clear
	echo "Sorry, wrong selection";;
	esac
	echo -en "\n\n\t\t\tHit any key to continue"
	read -n 1 line
done
clear

Make the file executable

chmod +x simplemenu.sh

Test

./simplemenu.sh

The output as shown in the featured image of this article.

See also:

1 thought on “Linux/Unix: Simple Menu Driven Program Using Shell Script”

  1. Worked like a charm...
    I'm placing you info in my scripts notes and giving you full credit...
    Thanks a

Comments are closed.