Bash的CRUD shell脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Bash的CRUD shell脚本相关的知识,希望对你有一定的参考价值。

Shell script written in bash which displays a menu giving the you ability to add, edit, modify, and delete users.
  1. #! /bin/bash
  2.  
  3. GREP=/bin/grep
  4.  
  5. createUser()
  6. {
  7. echo "Enter Username: "
  8. read name
  9.  
  10. $GREP -i $name /etc/passwd
  11.  
  12. if [ $? == 0 ]
  13. then
  14. echo "$name, is already a user"
  15. else
  16. echo "Preparing to create..."
  17. sleep 1
  18. clear
  19. echo "Enter a comment for $name"
  20. read comment
  21. sudo useradd -c "$comment" -m -s /bin/bash $name
  22. echo "User created!"
  23. less /etc/passwd | grep $name
  24. echo "Please supply a password for $name"
  25. sudo passwd $name
  26. fi
  27. }
  28.  
  29. modUser()
  30. {
  31. clear
  32. echo "Which user would you like to modify?"
  33. read modName
  34.  
  35. $GREP -i $modName /etc/passwd
  36.  
  37. if [ $? == 0 ]
  38. then
  39. echo "Would you like to:"
  40. echo "1) Add a comment for $modName"
  41. echo "2) Modify home directory"
  42. echo "3) Add an expiration date (yyyy-mm-dd)"
  43. echo "4) Exit"
  44.  
  45. read answer
  46. case "$answer" in
  47. 1) echo "Enter comment: ";
  48. read comment
  49. sudo usermod -c "$comment" $modName;
  50. less /etc/passwd | grep $modName;;
  51. 2) echo "Enter new home directoy [path]";
  52. read homeDir
  53. sudo usermod -d $homeDir $modName;
  54. less /etc/passwd | grep $modName;;
  55. 3) echo "Enter expiration date";
  56. read expiration
  57. sudo usermod -e $expiration $modName;
  58. sudo chage -l $modName;;
  59. 4) exit;;
  60. esac
  61. else
  62. echo "$modName does not exist!"
  63. fi
  64. }
  65.  
  66. deleteUser()
  67. {
  68. clear
  69. echo "Which user would you like to delete?"
  70. read delName
  71.  
  72. $GREP -i $delName /etc/passwd
  73.  
  74. if [ $? == 0 ]
  75. then
  76. echo "Are you sure you want to delete $delName ? [y/n]"
  77. read answer
  78. if [ $answer == "y"]
  79. then
  80. sudo userdel -r $delName
  81. echo "deleting..."
  82. sleep 1
  83. echo "$delName deleted!"
  84. exit 0
  85. fi
  86. else
  87. echo "$delName does not exist!"
  88. fi
  89.  
  90. }
  91.  
  92. clear
  93. echo "MENU"
  94. echo ""
  95. echo "1) Create User"
  96. echo "2) Modify User"
  97. echo "3) Delete User"
  98. echo "4) Exit"
  99.  
  100. read answer
  101.  
  102. case "$answer" in
  103. 1) createUser;;
  104. 2) modUser;;
  105. 3) deleteUser;;
  106. 4) exit;;
  107. esac

以上是关于Bash的CRUD shell脚本的主要内容,如果未能解决你的问题,请参考以下文章

Bash的变量类型

Shell脚本切割日志

如何执行bash脚本时,显示行数

代码片段:Shell脚本实现重复执行和多进程

Linux下如何执行Shell脚本

Java源代码中带有vimdiff命令的Shell脚本在Git bash上运行时卡住了