设计要求
在主shell脚本文件中(必须以menu命名)要有一个多操作选项的菜单以便用户从中选择。此菜单的主要内容有:
①显示当前所有用户的记录
②显示用户名和用户ID
③查询并显示特定用户的记录
④往passwd文件中增加新用户的记录
⑤从passwd文件中删除某个用户的记录
⑥···退出···
在目录中主要有四个文件,分别为:menu、passwd、add、delete。其中menu文件中的程序完成菜单1~3功能;add和delete脚本文件分别完成4、5。当用户做了选择且完成所选的操作后,可以再次显示主菜单以供用户做下一步选择。
要求:①用while循环和case条件语句完成本次课程设计
目录
add代码
echo "you are in the add"read -t 15 -p "please input your ID:" IDecho "your ID = $ID"echo -e "\n"read -t 15 -p "please input your name:" nameecho "your name = $name"echo -e "\n"read -t 30 -p "please input your statu:" statuecho "your name = $name"echo -e "\n"userinfo="$ID-$name-$statu"echo "$userinfo" >> passwd.txt
delete代码
echo "you are in the delete"echo "从passwd文件中删除某个用户的记录"read -t 30 -p "please input you want to delect ID:" IDa=1for line in `cat passwd.txt`doOLD_IFS="$IFS" #保存旧的分隔符IFS="-"arr=($line)IFS="$OLD_IFS" # 将IFS恢复成原来的if [ ${arr[0]} -eq $ID ]thensed -i "${a}d" passwd.txtfilet a+=1doneecho "success delete!!"echo "end show"~
passwd.txt代码
02-b-student03-c-teacher04-d-people05-g-student~
menu代码
function add(){sh addsleep 1}function delete(){sh deletesleep 1}function showall(){echo "All userinfo"for line in `cat passwd.txt`doecho "[===$line===]"doneecho "end show"sleep 1}function showuser(){echo "All ID and name"for line in `cat passwd.txt`doOLD_IFS="$IFS" #保存旧的分隔符IFS="-"arr=($line)IFS="$OLD_IFS" # 将IFS恢复成原来的echo "[**ID=${arr[0]}**name=${arr[1]}**]"doneecho "end show"sleep 1}function finduser(){echo "查询并显示特定用户的记录"read -t 30 -p "please input you want to find ID:" IDfor line in `cat passwd.txt`doOLD_IFS="$IFS" #保存旧的分隔符IFS="-"arr=($line)IFS="$OLD_IFS" # 将IFS恢复成原来的if [ ${arr[0]} -eq $ID ]thenecho "[**ID=${arr[0]}**name=${arr[1]}**statu=${arr[2]}**]"fidoneecho "end show"sleep 1}function menu(){cat << EOF************************************** MENU ** ** 1.add 2.delete ** 3.exit 4.showall ** 5.showuser 6.finduser ** ** author:509刘文豪 Don't copy! *************************************EOF}function usage(){read -p "please input your choice: " choicecase $choice in1)add;;2)delete;;3)exit 0;;4)showall;;5)showuser;;6)finduser;;esac}function main(){while truedomenuusagedone}main
自此功能所有都实现
小林博客










