一个很不错的sell菜单

发表于:2007-07-04来源:作者:点击数: 标签:
代码: #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, t

  代码:
  #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 must enter a mailid to send mail!"
        fi
        
          echo -n "Type your message: ^D to end: "
          mail ${mailid}
          echo " "
          echo " Press any key to continue..."
        ;;
  
       "2" | "r" | "R" )
        mail
        echo " "
        echo "Press any key to continue..."
        ;;
  
       "0" | "m" | "M" )
        break
        ;;
  
       * )
        MenuChoice=""
        MenuMsg="${BLD}Invalid Choice! Please try again.${OBLD}"
        ;;
     esac
    done
  } #end of MailMenu function
  
    
  #********************************************************************************
  #Routine UserUtilMenu
  
  UserUtilMenu()
  {
    MenuMsg=""
    while true
    do
     tput clear
     MenuChoice=""
  
     echo ""
     echo ""
     echo "${REV}USER UTILITY MENU${OREV}"
     echo " "
     echo " "
     echo "  1.  ${BLD}C${OBLD}hange Password"
     echo ""
     echo "  2.  ${BLD}D${OBLD}isplay Date and Time"
     echo ""
     echo "  3.  ${BLD}W${OBLD}ho is logged on"
     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" | "c" | "C" )
        ExecuteCommand 'passwd'
        ;;
  
       "2" | "d" | "D" )
        ExecuteCommand 'date'
        sleep 3
        ;;
  
       "3" | "w" | "W" )
        ExecuteCommand 'who'
        sleep 3
        ;;
  
       "0" | "m" | "M" )
        break
        ;;
  
       * )
        MenuChoice=""
        MenuMsg="${BLD}Invalid Choice! Please try again.${OBLD}"
        ;;
     esac
    done
  } # end of UserUtilMenu function
  
  
  #********************************************************************************
  #Routine FileDirUtilMenu
  
  FileDirUtilMenu()
  {
    MenuMsg=""
    while true 
    do
     tput clear
     MenuChoice=""
  
     echo ""
     echo ""
     echo "${REV}FILE / DIRECTORY UTILITY MENU${OREV}"
     echo ""
     echo ""
     echo "  1.  ${BLD}L${OBLD}ist files and directories"
     echo ""
     echo "  2.  ${BLD}C${OBLD}reate Directory"
     echo ""
     echo "  3.  ${BLD}R${OBLD}emove Directory"
     echo ""
     echo "  4.  ${BLD}D${OBLD}elete file"
     echo ""
     echo "  5.  Mo${BLD}v${OBLD}e file"
     echo ""
     echo "  6.  C${BLD}o${OBLD}py file"
     echo ""
     echo "  0.  ${BLD}M${OBLD}ain Menu"
     echo ""
     echo ""
     echo "${BLD} ${MenuMsg}${OBLD}"
     echo ""
     echo ""
     echo -n "${BLD} Select: ${OBLD}"
     echo ""
     MenuMsg=""
     read MenuChoice #get user selection
  
     case ${MenuChoice} in
       "1" | "l" | "L" )
        ExecuteCommand 'ls -l'
        ;;
  
       "2" | "c" | "C" )
        echo ""
        echo -n "Enter Directory name: "
        read dir
        if [ -d ${dir} ]
        then
          MenuMsg="${dir} - Directory already exists."
        else
          mkdir ${dir}
          echo ""
          echo "Press any key to continue.."
        fi
        ;;
  
       "3" | "r" | "R" )
        echo ""
        echo -n "Enter Directory name: "
        read dir
        if [ -d ${dir} ]
        then
          rmdir ${dir}
          if [ $? -eq 0 ]
          then
           echo ""
           echo "Press any key to continue.."
          else
           MenunMessage="${dir} - Error removing directory. (Not empty)."
          fi
        else
          MenuMsg="${BLD}${dir} - Directory does not exist.${OBLD}"
        fi
        ;;
  
       "4" | "d" | "D" )
        echo ""
        echo -n "Enter File name: "
        read filename
        if [ -f $filename ]
        then
          rm ${filename}
          if [ $? -eq 0 ]
          then
           :
          else
           MenuMsg="${BLD}${filename} - Error removing file.${OBLD}"
          fi
          echo ""
          echo "Press any key to continue.."
        else
          MenuMsg="${BLD}${filename} - File does not exist.${OBLD}"
        fi
        ;;
  
       "5" | "v" | "V" )
        echo ""
        echo -n "Enter source file name: "
        read srcfile
        if [ ! -f $srcfile ]
        then
          MenuMsg="${BLD}${srcfile} - file does not exist.${OBLD}"
          continue
        fi
        echo ""
        echo -n "Enter target file name: "
        read targetfile
        if [ -f ${targetfile} ]
        then
          echo ""
          echo "${targetfile} already exists"
          echo -n "Overwrite (y/n) "
          read move
        else
          move="y"
        fi
        if [ "${move}" = "y" -o "${move}" = "Y" ]
        then
          mv ${srcfile} ${targetfile}
          if [ $? -eq 0 ]
          then
           :
          else
           MenuMsg="${BLD}${filename} - Error moving file.${OBLD}"
          fi
          echo ""
          echo "Press any key to continue.."
        fi
        ;;
  
       "6" | "o" | "O" )
        echo ""
        echo -n "Enter source file name: "
        read srcfile
        if [ ! -f $srcfile ]
        then
          MenuMsg="${BLD}${filename} - file does not exist.${OBLD}"
          continue
        fi
        echo ""
        echo -n "Enter target file name: "
        read targetfile
        if [ -f $targetfile ]
        then
          echo ""
          echo "${targetfile} already exists"
          echo -n "Overwrite (y/n): "
          read copy
        else
          copy="y"
        fi
        if [ "${copy}" = "y" -o "${copy}" = "Y" ]
        then
          cp ${srcfile} ${targetfile}
          if [ $? -eq 0 ]
          then
             :
          else
           MenuMsg="${BLD}${filename} - Error copying file.${OBLD}"
          fi
          echo ""
          echo "Press any key to continue.."
        fi
        ;;
  
       "0" | "m" | "M" )
        break
        ;;
  
       * )
        MenuChoice=""
        MenuMsg="${BLD}Invalid Choice! Please try again${OBLD}"
        ;;
     esac
    done
  } #end of FileDirUtilMenu function
  
  
  #********************************************************************************
  #Routine GetFileNama
  #Displays directory listing associated with an array of numbers. File is selected by
  #selecting number associated with the file name.
  GetFileName()
  {
  #Variables
    typeset -i Index
    typeset -i IndexIncrement
    typeset -i MaxIndex
    IndexIncrement=1
    Index=0
  
  #executes ls and loads all files and directories into an array
    for i in $(ls)
    do
     FileName[Index]="${i}"
     Index=Index+IndexIncrement
    done
  
  #sets MaxIndex to the number of files returned
    MaxIndex=Index-IndexIncrement
    Index=0
  
  #Displays the results to the stdout
    tput clear
    MenuChoice=""
  
    echo ""
    echo "${BLD}Item File Name${OBLD}"
    echo "____ _____________________________________________"
  
  #adjusts spacecing variable to make sure columns are aligned when index is 2 digits
    while true
    do
     if [ "${Index}" -lt 10 ]
     then
       spcl=" "
     else
       spcl=""
     fi
  
     echo "${Index}${spcl}) ${FileName[$Index]} "
     Index=Index+IndexIncrement
  
     if [ "${Index}" -gt "${MaxIndex}" ]
     then
       break
     fi
    done
  
    echo -n "Enter Item Number of File to use: "
    read MenuChoice #get user selection
    if [ -z "${MenuChoice}" ]
    then
     echo "Invalid entry!"
     sleep 2
     return 1
    fi
  
    if [ "${MenuChoice}" -gt "${MaxIndex}" ]
    then
     echo "Invalid Entry : ${MenuChoice}"
     sleep 2
     return 1
    fi
  
    eval "${1}=${FileName[$(expr $MenuChoice)]}"
    return 0
  } #end of GetFileName function
  
  #********************************************************************************
  #Routine ExecuteCommand
  ExecuteCommand()
  {
    tput clear
    echo " "
    echo " "
    echo " "
    $1
    echo " "
    echo "Press any key to continue.."
    return
  }
  
  MainMenu

原文转自:http://www.ltesting.net