linux简易的用户信息管理系统

自动草稿

自动草稿
自动草稿
自动草稿

设计要求

在主shell脚本文件中(必须以menu命名)要有一个多操作选项的菜单以便用户从中选择。此菜单的主要内容有:
①显示当前所有用户的记录
②显示用户名和用户ID
③查询并显示特定用户的记录
④往passwd文件中增加新用户的记录
⑤从passwd文件中删除某个用户的记录
⑥···退出···
在目录中主要有四个文件,分别为:menu、passwd、add、delete。其中menu文件中的程序完成菜单1~3功能;add和delete脚本文件分别完成4、5。当用户做了选择且完成所选的操作后,可以再次显示主菜单以供用户做下一步选择。
要求:①用while循环和case条件语句完成本次课程设计

目录

在这里插入图片描述

add代码

  1. echo "you are in the add"
  2. read -t 15 -p "please input your ID:" ID
  3. echo "your ID = $ID"
  4. echo -e "\n"
  5. read -t 15 -p "please input your name:" name
  6. echo "your name = $name"
  7. echo -e "\n"
  8. read -t 30 -p "please input your statu:" statu
  9. echo "your name = $name"
  10. echo -e "\n"
  11. userinfo="$ID-$name-$statu"
  12. echo "$userinfo" >> passwd.txt

delete代码

  1. echo "you are in the delete"
  2. echo "从passwd文件中删除某个用户的记录"
  3. read -t 30 -p "please input you want to delect ID:" ID
  4. a=1
  5. for line in `cat passwd.txt`
  6. do
  7. OLD_IFS="$IFS" #保存旧的分隔符
  8. IFS="-"
  9. arr=($line)
  10. IFS="$OLD_IFS" # 将IFS恢复成原来的
  11. if [ ${arr[0]} -eq $ID ]
  12. then
  13. sed -i "${a}d" passwd.txt
  14. fi
  15. let a+=1
  16. done
  17. echo "success delete!!"
  18. echo "end show"
  19. ~

passwd.txt代码

  1. 02-b-student
  2. 03-c-teacher
  3. 04-d-people
  4. 05-g-student
  5. ~

menu代码

  1. function add()
  2. {
  3. sh add
  4. sleep 1
  5. }
  6. function delete()
  7. {
  8. sh delete
  9. sleep 1
  10. }
  11. function showall()
  12. {
  13. echo "All userinfo"
  14. for line in `cat passwd.txt`
  15. do
  16. echo "[===$line===]"
  17. done
  18. echo "end show"
  19. sleep 1
  20. }
  21. function showuser()
  22. {
  23. echo "All ID and name"
  24. for line in `cat passwd.txt`
  25. do
  26. OLD_IFS="$IFS" #保存旧的分隔符
  27. IFS="-"
  28. arr=($line)
  29. IFS="$OLD_IFS" # 将IFS恢复成原来的
  30. echo "[**ID=${arr[0]}**name=${arr[1]}**]"
  31. done
  32. echo "end show"
  33. sleep 1
  34. }
  35. function finduser()
  36. {
  37. echo "查询并显示特定用户的记录"
  38. read -t 30 -p "please input you want to find ID:" ID
  39. for line in `cat passwd.txt`
  40. do
  41. OLD_IFS="$IFS" #保存旧的分隔符
  42. IFS="-"
  43. arr=($line)
  44. IFS="$OLD_IFS" # 将IFS恢复成原来的
  45. if [ ${arr[0]} -eq $ID ]
  46. then
  47. echo "[**ID=${arr[0]}**name=${arr[1]}**statu=${arr[2]}**]"
  48. fi
  49. done
  50. echo "end show"
  51. sleep 1
  52. }
  53. function menu()
  54. {
  55. cat << EOF
  56. *************************************
  57. * MENU *
  58. * *
  59. * 1.add 2.delete *
  60. * 3.exit 4.showall *
  61. * 5.showuser 6.finduser *
  62. * *
  63. * author:509刘文豪 Don't copy! *
  64. ************************************
  65. EOF
  66. }
  67. function usage()
  68. {
  69. read -p "please input your choice: " choice
  70. case $choice in
  71. 1)
  72. add
  73. ;;
  74. 2)
  75. delete
  76. ;;
  77. 3)
  78. exit 0
  79. ;;
  80. 4)
  81. showall
  82. ;;
  83. 5)
  84. showuser
  85. ;;
  86. 6)
  87. finduser
  88. ;;
  89. esac
  90. }
  91. function main()
  92. {
  93. while true
  94. do
  95. menu
  96. usage
  97. done
  98. }
  99. main

自此功能所有都实现

© 版权声明
THE END
喜欢就支持一下吧
点赞8赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容