• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

一个很不错的sell 菜单

发布: 2007-6-08 22:43 | 作者: seanhe | 来源: | 查看: 19次 | 进入软件测试论坛讨论

领测软件测试网
http://unix.ittoolbox.com/code/d.asp?d=2415&a=s
作者:amf57_unix


[code:1:035f8f0128]#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 
[/code:1:035f8f0128]

 zg3879 回复于:2004-05-22 07:36:12
这个shell是干什么用的,怎么也没有功能介绍!

 simonzhan 回复于:2004-05-22 09:24:31
这是一个shell的通用菜单模板,你可以在上面添加自己的应用.

 htldm 回复于:2004-05-22 14:05:47
不错!很通用。

 zfl 回复于:2004-05-22 21:08:25
先收藏.

 jerry111 回复于:2004-05-23 08:09:39
谢谢楼主分享

 erjing 回复于:2004-05-23 21:18:04
谢谢!

 li2002 回复于:2004-05-24 08:11:20
蛮不错的,有学习的地方

 sdccf 回复于:2004-05-24 08:38:59
调用一些常用的命令和系统管理功能。

 绣林 回复于:2004-05-24 08:49:54
很通用,不错。

 lurchin 回复于:2004-05-24 16:27:14
不错,楼主辛苦了

 海德 回复于:2004-05-24 18:39:34
收藏!!
好东东
 :)

 moomoon 回复于:2004-05-25 17:56:29
xie xie

 qinjian1981 回复于:2004-05-26 13:17:13
先收到收藏夹吧
谢谢了

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网