代码:
#Program: mmenu - shell script of user menu items
#Description:
# Program consisting of a series of functions providing user menus
# for simple tasks.
#
# Functions are loaded into memory and available for execution. Once
# loaded, the main menu will be invoked. The main menu (entry point) is :MainMenu.
#
# The following functions are declared:
# MainMenu
# EditFile
# MailMenu
# UserUtilMenu
# FileDirUtilMenu
# GetFileName
# ExecuteCommand
#
# Last updated on 10/10/2002 by A.M. Foster
#************************************************************8
#Screen Display Variables
REV=`tput rev`
OREV=`tput sgr0`
BLD=`tput smso`
OBLD=`tput rmso`
#Various variables
MenuMsg=""
MenuChoice=""
#*********************************************************************8
#Routine: MainMenu
#Main Menu function which displays the main menu to stdout.
MainMenu()
{
#Menu loop
while true
do
tput clear
MenuChoice=""
#Main Menu - display the main menu
echo ""
echo ""
echo " ${REV}Main Menu${OREV}"
echo ""
echo ""
echo " 1. ${BLD}E${OBLD}dit / File "
echo ""
echo " 2. ${BLD}S${OBLD}end/Receive Mail ..."
echo ""
echo " 3. ${BLD}U${OBLD}ser Utilities ..."
echo ""
echo " 4. ${BLD}F${OBLD}ile Directory Utilities ..."
echo ""
echo " 0. ${BLD}L${OBLD}og Off System"
echo ""
echo ""
echo "${BLD}${MenuMsg}${OBLD}"
echo ""
echo ""
echo -n "${BLD}Select: ${OBLD}"
MenuMsg=""
#Get user's choice and evaluate with case statement
read MenuChoice #get user selection
case ${MenuChoice} in
"1" | "e" | "E" )
EditFile
;;
"2" | "s" | "S" )
MailMenu
;;
"3" | "u" | "U" )
UserUtilMenu
;;
"4" | "f" | "F" )
FileDirUtilMenu
;;
"0" | "l" | "L" )
echo " "
echo " "
echo "Exiting per User request!"
sleep 2
tput clear
exit 1
;;
* )
MenuChoice=""
MenuMsg="${BLD}Invalid choice. Please try again.${OBLD}"
;;
#Example of menu command that is hidden from users. Note that
#it is not in the menu, but has a choice in the case statement.
"admin" )
ExecuteCommand 'ps -ef | more'
;;
esac #end of case statement
done #end of menu loop
}
#************************************************************************************
#Routine EditFile
#Presents the Edit File Menu to stdout
EditFile()
{
MenuMsg=""
while true
do
MenuChoice=""
tput clear
echo ""
echo ""
echo "${REV}EDIT FILE${OREV}"
echo ""
echo ""
echo "1. ${BLD}E${OBLD}nter File Name "
echo ""
echo "2. Enter File Name Name from Li${BLD}s${OBLD}t ... "
echo ""
echo "0. ${BLD}M${OBLD}ain Menu "
echo ""
echo ""
echo "${BLD}${MenuMsg}${OBLD}"
echo ""
echo ""
echo -n "${BLD}Select: ${OBLD}"
MenuMsg=""
#Get user's choice and evaluate with case statement
read MenuChoice #get user selection
case ${MenuChoice} in
"1" | "e" | "E" )
echo -n "Enter file name to edit: "
read FileName
if [ -z "${FileName}" ] #check if user provided a file name
then
MenuMsg="You did not enter a valid file name!"
else
if [ -f ${FileName} ] #check if file exists
then
#check if file is a text file
if [ "file ${FileName} | grep text >> /dev/null" ]
then
filesoktoedit="y"
else
MenuMsg="${FileName} - is not an editable file!"
fi
else
filesoktoedit="y"
fi
fi
if [ $}filesoktoedit}="y" ]
then
vi "${FileName}"
echo " "
echo " Press any key to continue.."
fi
;;
"2" | "s" | "S" )
FileName=""
GetFileName FileName
if [ $? -eq 0 ]
then
if [ "file ${FileName} | grep text >> /dev/null" ]
then
vi "${FileName}"
echo " "
echo " Press any key to continue.."
else
MenuMsg="${BLD}${FileName} - is not an editable file!${OBLD}1"
fi
fi
;;
"0" | "m" | "M" )
break
;;
* )
MenuChoice=""
MenuMsg="${BLD}Invalid Choice! Please try again.${OBLD}"
;;
esac
done
return 0
} #end of EditFile function
#**********************************************************************************
#Routine MailMenu
#Displays Mail Menu to stdout
MailMenu()
{
MessageMenu=""
while true
do
tput clear
MenuChoice=""
echo ""
echo ""
echo "${REV}MAIL MENU${OREV}"
echo ""
echo ""
echo " 1. ${BLD}S${OBLD}end Mail"
echo ""
echo " 2. ${BLD}R${OBLD}eceive Mail"
echo ""
echo " 0. ${BLD}M${OBLD}ain Menu"
echo ""
echo "${BLD}${MenuMsg}${OBLD}"
echo ""
echo ""
echo -n "${BLD}Select: ${OBLD}"
MenuMsg=""
#Get user's choice and evaluate with case statement
read MenuChoice #get user selection
case ${MenuChoice} in
"1" | "s" | "S" )
echo " "
echo -n "Enter a mail ID to send to: "
read mailid
if [ -z "${mailid}" ]
then
MenuMsg="You mus