OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Menus

A common type of interactivity is called menu-driven. In menu-driven programs, the user is presented with a list of choices and is asked to choose one. For example, we could imagine a program that presented the following:



Please Select:


1. Display System Information

2. Display Disk Space

3. Display Home Space Utilization

0. Quit


Enter selection [0-3] >

Please Select:


1. Display System Information

2. Display Disk Space

3. Display Home Space Utilization

0. Quit


Enter selection [0-3] >


Using what we learned from writing our sys_info_page program, we can construct a menu-driven program to perform the tasks on the above menu:



#!/bin/bash

# read-menu: a menu driven system information program clear

echo "

Please Select:


1. Display System Information

2. Display Disk Space

3. Display Home Space Utilization

0. Quit "

read -p "Enter selection [0-3] > "

#!/bin/bash

# read-menu: a menu driven system information program clear

echo "

Please Select:


1. Display System Information

2. Display Disk Space

3. Display Home Space Utilization

0. Quit "

read -p "Enter selection [0-3] > "


if [[ $REPLY =~ ^[0-3]$ ]]; then if [[ $REPLY == 0 ]]; then

echo "Program terminated." exit

fi

if [[ $REPLY == 1 ]]; then echo "Hostname: $HOSTNAME" uptime

exit

fi

if [[ $REPLY == 2 ]]; then df -h

exit

fi

if [[ $REPLY == 3 ]]; then

if [[ $(id -u) -eq 0 ]]; then

echo "Home Space Utilization (All Users)" du -sh /home/*

else

echo "Home Space Utilization ($USER)" du -sh $HOME

fi exit

fi else

echo "Invalid entry." >&2 exit 1

fi

if [[ $REPLY =~ ^[0-3]$ ]]; then if [[ $REPLY == 0 ]]; then

echo "Program terminated." exit

fi

if [[ $REPLY == 1 ]]; then echo "Hostname: $HOSTNAME" uptime

exit

fi

if [[ $REPLY == 2 ]]; then df -h

exit

fi

if [[ $REPLY == 3 ]]; then

if [[ $(id -u) -eq 0 ]]; then

echo "Home Space Utilization (All Users)" du -sh /home/*

else

echo "Home Space Utilization ($USER)" du -sh $HOME

fi exit

fi else

echo "Invalid entry." >&2 exit 1

fi


This script is logically divided into two parts. The first part displays the menu and inputs the response from the user. The second part identifies the response and carries out the se- lected action. Notice the use of the exit command in this script. It is used here to pre- vent the script from executing unnecessary code after an action has been carried out. The presence of multiple exit points in a program is generally a bad idea (it makes program logic harder to understand), but it works in this script.


Top OS Cloud Computing at OnWorks: