← Archive
article· jaw

Bash script with menu example

bash

#!/bin/bash

# Description:

# This script generates a menu program with one sub-menu.

# The options are explained beside their code.

 

 

# Declare variable EXIT and RETURN

RETURN=Y

EXIT=N

 

# Initiate Main Menu loop

while [ $EXIT = 'N' ]

do

RETURN=Y

# Change background color to blue.

setterm -background blue

 

# Clear screen.

clear

 

# Display Main menu using cat command.

cat <<++

Main Menu

 

A) List Files in Current Directory

B) Display information about a user

C) Display File

D) Utility Sub Menu

E) Display User Name

X) Exit

++

 

# Prompt user for a selection

echo -e "Please enter your choice $LOGNAME: \c"

#echo "Press enter key"

read SELECTION

echo

echo -e  "You chose \c"

setterm -blink on 

echo -e "$SELECTION\n"

setterm -blink off

 

# Initiate case selection structure to decode users choice

case "$SELECTION" in

a|A)

# This choice displays the contents of the current directory.

echo -e "Here are the contents of the current directory\n\n"

ls -l | more

;;

b|B)

# This choice will display a list of all users if l or L is pressed or

# will run the finger, who and ps commands filtered to display

# information about the user name that is entered.

echo -e "Type L to display a list of all users online."

echo -e "Enter the name of the user you want to check up on: \c"

read NAME

if [ $NAME = 'L' ] || [ $NAME = 'l' ]

then

who | more

else

finger $NAME | more

who | grep $NAME | more

echo -e "The following is a list of all processes involving"

echo -e "the selected user.\n"

echo -e "\nPress enter to continue\n"

read PAUSE

ps -aux | grep $NAME | more

        fi 

 

 

 

;;

 

 

c|C)

# This choice will prompt the user for a file name the display its contents

# if it exists or an error if it doesn't

echo -e "Enter file name to display: \c"

read FILENAME

if [ -f $FILENAME ]

then

echo -e "The contents of $FILENAME is \n\n" 

cat $FILENAME | more

else

echo "The file $FILENAME does not exist!"

fi

 

;;

 

d|D)

# This choice will move the user to the utility sub menu

EXIT2=n

while [ $EXIT2 = 'n' ]

do

setterm -background magenta

        clear

cat <<++

 

 

Sub Menu

1) Date

2) Calendar

3) Change your password

Q) Return to Main Menu

X) Exit 

++

echo -e "\n\nEnter your choice $LOGNAME: \c"

read SELECTION2

echo -e "\nYour choice was \c"

setterm -bold on

echo -e "$SELECTION2"

setterm -bold off

# This sub menu uses if elif and else to decode the users choice.

# This choice will display the date.

if [ $SELECTION2 = '1' ]

then

echo -e "\nThe Date is"

date

elif [ $SELECTION2 = '2' ]

# This selection prompts the user for a year then displays the calendar for

# that year.

        then

echo -e "\n\nEnter the year of the calendar you want to see :\c"

read YEAR

echo -e "\n"

cal $YEAR | more

 

elif [ $SELECTION2 = '3' ]

# This choice will run the passwd utility for changing your password. 

then 

echo -e "\n\n\n"

passwd

 

elif [ $SELECTION2 = 'q' ] || [ $SELECTION2 = 'Q' ]

# This choice will return you to the main menu after two returns.

then

echo -e "\n Returning to main menu\n"

                RETURN=N

EXIT2=Y

 

elif [ $SELECTION2 = 'x' ] || [ $SELECTION2 = 'X' ]

# This choice will exit the menu program.

then

echo -e "\n\nLeaving Jay's virtual environment"

RETURN=N

                EXIT2=Y 

EXIT=Y

 

else

# This is the default option if an incorrect keystroke is entered.

echo -e "That's not an option at this juncture"

 

fi

                if [ $RETURN = 'Y' ]

                then

echo -e "\n\nPress return to continue\c"

read PAUSE

                else

sleep 1

                fi

done

;;

# This choice will will display the users login name with a twist.

e|E)

echo -e "And the winner is.\c"

sleep 1

echo -e ".\c"

sleep 1

echo -e ".\c"

sleep 1

echo -e ".\c"

sleep 1

echo -e ".\c"

setterm -blink on

echo -e "\n        $LOGNAME"

setterm -blink off

;;     

# This choice will exit the menu program.

x|X) 

 

echo -e "You are now leaving Jay's virtual environment.....\n\n\n"

        RETURN=N

        EXIT=Y

 

;;

# This is the default option if an incorrect keystroke is entered.

*)

echo -e "Still learning to type?????\n\n\n\n"

;;

# This is the end of the case structure.

esac  

# This prompts the user to press enter to continue so that they will have

# a chance to see what is on the screen before the clear screen command at

# the start of the loop wipes the screen

if [ $RETURN = 'Y' ]

then

echo -e "\n\nPress ENTER to continue\c"

read PAUSE

else

sleep 1

fi

done

 

 

 

 

 

 

 

 

Tags:

Restored from the original jawtech.com (archived copy). Links and downloads from that era may no longer work.